Skip to content

Instantly share code, notes, and snippets.

@terakun
Created March 26, 2015 12:37
Show Gist options
  • Save terakun/360479e2c40fad6218ee to your computer and use it in GitHub Desktop.
Save terakun/360479e2c40fad6218ee to your computer and use it in GitHub Desktop.
切り餅発見伝
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
typedef std::vector<std::string> Table;
typedef unsigned long long int ullint;
ullint rectnum(const Table&);
void subnum(const Table&,unsigned,unsigned,ullint&);
int main(int argc,char **argv){
std::ifstream ifs("in.txt");
Table table;
std::string in;
unsigned n,m;
ifs>>n>>m;
// std::cin>>n>>m;
for(int i=0;i<n;++i){
ifs>>in;
// std::cin>>in;
table.push_back(in);
// table.push_back(std::string(m,'#'));
}
std::cout<<rectnum(table)<<std::endl;
return 0;
}
ullint rectnum(const Table& table){
ullint num=0;
unsigned maxy=table.size()-2;
unsigned maxx=table[0].size()-2;
for(unsigned y=0;y<maxy;++y){
for(unsigned x=0;x<maxx;++x){
if(table[y][x]=='#'&&table[y+1][x]=='#'&&table[y][x+1]=='#'
&&table[y+2][x]=='#'&&table[y][x+2]=='#')
subnum(table,x,y,num);
}
}
return num;
}
void subnum(const Table& table,unsigned px,unsigned py,ullint& num){
unsigned maxx,maxy;
unsigned i,x,y;
for(maxx=2;maxx<(table[0].size()-px)&&table[py][px+maxx]=='#';maxx++);
for(maxy=2;maxy<(table.size()-py)&&table[py+maxy][px]=='#';maxy++);
for(y=2;y<maxy;++y){
for(x=0;x<maxx&&table[py+y][px+x]=='#';++x){
if(x>=2){
for(i=0;i<y&&table[py+i][px+x]=='#';++i);
if(i==y)num++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment