From fd2d459e2cdcaf96d88cbe0aa63b080a7db53741 Mon Sep 17 00:00:00 2001 From: KiritoTRw <3021577574@qq.com> Date: Sun, 1 May 2016 15:04:24 +0800 Subject: [PATCH] Create 1031.cpp --- QUSTOJ/1031.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 QUSTOJ/1031.cpp diff --git a/QUSTOJ/1031.cpp b/QUSTOJ/1031.cpp new file mode 100644 index 0000000..60af51c --- /dev/null +++ b/QUSTOJ/1031.cpp @@ -0,0 +1,79 @@ +#include +using namespace std; +#define MAXR 1005 +#define MAXC 1005 + +bool vis[MAXR][MAXC]; +char mapx[MAXR][MAXC]; + +void dfs(int y,int x) +{ + if(vis[y][x]||mapx[y][x]==0||mapx[y][x]=='.') return; + vis[y][x]=true; + dfs(y-1,x); + dfs(y+1,x); + dfs(y,x-1); + dfs(y,x+1); +} +bool checkmap(int R,int C) +{ + for(int i=1;i<=R;i++) + { + for(int j=1;j<=C;j++) + { + if(mapx[i][j]=='#') + { + if(mapx[i-1][j]=='#') + { + if(mapx[i][j-1]=='#') + { + if(mapx[i-1][j-1]=='#') + { + continue; + } + else + { + return true; + } + } + } + else + { + if (mapx[i][j - 1] == '#' && mapx[i - 1][j - 1] == '#') + { + return true; + } + } + } + } + } + return false; +} +int main() +{ + int R,C; + scanf("%d %d",&R,&C); + for(int i=1;i<=R;i++) + { + scanf("%s",&mapx[i][1]); + } + if(checkmap(R,C)) + { + printf("Bad placement.\n"); + return 0; + } + int cnt=0; + for(int i=1;i<=R;i++) + { + for(int j=1;j<=C;j++) + { + if(mapx[i][j]=='#'&&vis[i][j]==false) + { + dfs(i,j); + cnt++; + } + } + } + printf("There are %d ships.\n",cnt); + return 0; +}