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): 10286    Accepted Submission(s): 6426

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.

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)

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
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#[email protected]#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
[email protected]
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13

Source

Asia 2004, Ehime (Japan), Japan Domestic

做好细节工作,迫在眉睫!!!本来测试数据三组都对了,最后一组怎么都不对,调试了一中午,才发现是map数组忘了初始化了。。。。

代码如下:

#include<stdio.h>
#include<string.h>
char map[1001][1001];
int visit[1001][1001];
int count,n,m;
void dfs(int a,int b)
{
	if(a<0||b<0||a>=1001||b>=1001)
	return ;
	if(!visit[a-1][b]&&map[a-1][b]=='.')
	{
		visit[a-1][b]=1;
		count++;
		dfs(a-1,b);
	//	visit[n-1][m]=0;
	}
	if(!visit[a][b-1]&&map[a][b-1]=='.')
	{
		visit[a][b-1]=1;
		count++;
		dfs(a,b-1);
	//	visit[n][m-1]=0;
	}
	if(!visit[a+1][b]&&map[a+1][b]=='.')
	{
		visit[a+1][b]=1;
		count++;
		dfs(a+1,b);
	//	visit[n+1][m]=0;
	}
	if(!visit[a][b+1]&&map[a][b+1]=='.')
	{
		visit[a][b+1]=1;
		count++;
		dfs(a,b+1);
	//	visit[n][m+1]=0;
	}
	else
		return ;
}
int main()
{
	int i,j;
	while(~scanf("%d%d",&n,&m),n||m)
	{
		count=1;
		memset(visit,0,sizeof(visit));
		memset(map,'#',sizeof(map));//这个一定要加上,就因为这个疏忽,我足足调试了一中午。。。
		for(i=0;i<m;++i)
		{
			scanf("%s",map[i]);
		}
		for(i=0;i<m;++i)
		{
			for(j=0;j<n;++j)
			{
				if(map[i][j]=='@')
				{
					dfs(i,j);
					printf("%d\n",count);
					break;
				}
			}
		}
	}
	return 0;
} 
时间: 2024-10-29 19:08:14

hdu 1312 Red and Black(深搜)的相关文章

HDU 1312 ----- Red and Black 入门搜索 DFS解法

HDU 1312 ----- Red and Black  入门搜索  http://acm.hdu.edu.cn/showproblem.php?pid=1312 /*HDU 1312 ----- Red and Black 入门搜索 */ #include <cstdio> int n, m; //n行m列 int cnt, startx, starty; char mapp[25][25]; /*找一个连通的区域里可以走的块*/ void dfs(int x, int y){ if (x

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): 9795    Accepted Submission(s): 6103 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

HDU 1016 素数环(深搜)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25134 Accepted Submission(s): 11222 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1,

HDU 2553 N皇后问题(深搜DFS)

N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 772   Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于给定的N,求出

HDU 2102 A计划 (深搜)

Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚,告招天下勇士来拯救公主.不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出. 现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示.骑士们一进入时空传输机就会被转到另一层的相对位

HDU 1312:Red and Black(DFS搜索)

HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u 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

HDU 1312 Red and Black (dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17773    Accepted Submission(s): 10826 Problem Description There is a rectangula

Red and Black 深搜

 B - Red and Black Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Description There is a rectangular room, covered with square tiles. Each tile is colored either red

HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)

题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20820    Accepted Submission(s): 12673 Problem Description There i