标签:return start plm spec black which positive mis square
题目网址:http://poj.org/problem?id=1979
题目:
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 36033 | Accepted: 19517 |
Description
Input
Output
Sample Input
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
Sample Output
45 59 6 13
思路:
一道很简单的搜索题,用DFS和BFS都能做。不需要重复走一个点,走过一个点就把它设为红瓷砖,不用再走了。
代码:
1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 int n,m; 5 char tiles[25][25]; 6 int xb,yb; 7 int dirx[4]={0,0,1,-1};//行方向数组 8 int diry[4]={1,-1,0,0};//列方向数组 9 int res; 10 bool check(int x,int y){ 11 if(x<0 || x>=n) return false;//判断是否超边界 12 if(y<0 || y>=m) return false; 13 if(tiles[x][y]!=‘.‘) return false;//判断是否为黑瓷砖 14 return true; 15 } 16 void dfs(int x,int y){ 17 for(int d=0,xx,yy; d<4; d++){ 18 xx=x+dirx[d]; 19 yy=y+diry[d]; 20 if(check(xx, yy)){ 21 res++; 22 tiles[xx][yy]=‘#‘;//将走过的点都设为红瓷砖,就不会再走 23 dfs(xx, yy); 24 } 25 } 26 } 27 int main(){ 28 int flag; 29 while(scanf("%d%d ",&m,&n)==2 && (n+m)){ 30 flag=0; 31 res=1; 32 for (int i=0; i<n; i++) { 33 gets(tiles[i]); 34 for (int j=0; j<m && !flag; j++) { 35 if(tiles[i][j]==‘@‘){//寻找起点 36 xb=i; 37 yb=j; 38 flag=1; 39 break; 40 } 41 } 42 } 43 tiles[xb][yb]=‘#‘; 44 dfs(xb,yb); 45 printf("%d\n",res); 46 } 47 return 0; 48 }
标签:return start plm spec black which positive mis square
原文地址:http://www.cnblogs.com/uniles/p/7150660.html