首页 > 代码库 > hdu 1312 Red and Black
hdu 1312 Red and Black
Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10133 Accepted Submission(s): 6321
Problem Description
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can‘t move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
‘.‘ - a black tile
‘#‘ - a red tile
‘@‘ - a man on a black tile(appears exactly once in a data set)
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
‘.‘ - a black tile
‘#‘ - a red tile
‘@‘ - a man on a black tile(appears exactly once in a data set)
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
0 0
Sample Output
45
Source
Asia 2004, Ehime (Japan), Japan Domestic
PS:简单题
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstdlib> 5 #include<cstring> 6 #include<queue> 7 using namespace std; 8 int n,m; 9 int xi,yj;10 char map[25][25];11 int vis[25][25];12 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上下左右13 struct node{14 int x;15 int y;16 };17 18 bool check(int x,int y)19 {20 if(x>=1&&x<=n && y>=1&&y<=m && map[x][y]==‘.‘ && !vis[x][y])21 return true;22 return false;23 }24 25 int bfs()26 {27 memset(vis,0,sizeof(vis));28 int i,sum=1;29 node p,q;30 queue<struct node>Q;31 p.x=xi;32 p.y=yj;33 Q.push(p);34 while(!Q.empty())35 {36 p=Q.front();37 Q.pop();38 39 for(i=0;i<4;i++)40 {41 q.x=p.x+dir[i][0];42 q.y=p.y+dir[i][1];43 if(check(q.x,q.y))44 {45 vis[q.x][q.y]=1;46 sum++;47 Q.push(q);48 }49 }50 }51 return sum;52 }53 54 55 int main()56 {57 int i,j;58 int count;59 //freopen("in.txt","r",stdin);60 while(~scanf("%d%d",&m,&n))61 {62 63 if(!n&&!m)64 break;65 for(i=1;i<=n;i++)66 {67 getchar();68 for(j=1;j<=m;j++)69 {70 scanf("%c",&map[i][j]);71 if(map[i][j]==‘@‘)72 {73 xi=i;74 yj=j;75 }76 }77 }78 count=bfs();79 printf("%d\n",count);80 }81 return 0;82 }
hdu 1312 Red and Black
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。