poj1979(回溯)

原创 2018年04月16日 19:32:59
Red and Black
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 41029 Accepted: 22258

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) 
The end of the input is indicated by a line consisting of two zeros. 

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
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6

13

#include<iostream>
using namespace std;
char map[25][25];
int visited[25][25];
int w,h,ans,ww,hh;
void search(int x,int y)
{
	if(x>=0&&x<h&&y>=0&&y<w&&!visited[x][y]&&map[x][y]=='.')
	{
		visited[x][y]=1;
		ans++;
		search(x,y-1);
		search(x,y+1);
		search(x-1,y);
		search(x+1,y);
	}
 } 
int main()
{
	while(cin>>w>>h&&(w||h))
	{
		ans=0;
		for(int i=0;i<h;i++)
		{
			for(int j=0;j<w;j++)
			{
				cin>>map[i][j];
				visited[i][j]=0;
			}
		}
		for(int i=0;i<h;i++)
		{
			for(int j=0;j<w;j++)
			{
				if(map[i][j]=='@') 
				{
					hh=i;
					ww=j;
					map[i][j]='.';
				}
			}
		}
		search(hh,ww);
		for(int i=0;i<h;i++)
		{
			for(int j=0;j<w;j++)
			{
				
				cout<<visited[i][j];
			}
			cout<<endl;
		}
		cout<<ans<<endl;
	}
	return 0;
}
#include<iostream>
using namespace std;
char map[25][25];
int visited[25][25];
int w,h,ans,ww,hh;
void search(int x,int y)
{
	if(x<0||x>=h||y<0||y>=w||visited[x][y]||map[x][y]=='#') return ;
		visited[x][y]=1;
		ans++;
		search(x,y-1);
		search(x,y+1);
		search(x-1,y);
		search(x+1,y);
 } 
int main()
{
	while(cin>>w>>h&&(w||h))
	{
		ans=0;
		for(int i=0;i<h;i++)
		{
			for(int j=0;j<w;j++)
			{
				cin>>map[i][j];
				visited[i][j]=0;
			}
		}
		for(int i=0;i<h;i++)
		{
			for(int j=0;j<w;j++)
			{
				if(map[i][j]=='@') 
				{
					hh=i;
					ww=j;
				}
			}
		}
		search(hh,ww);
	/*	for(int i=0;i<h;i++)
		{
			for(int j=0;j<w;j++)
			{
				
				cout<<visited[i][j];
			}
			cout<<endl;
		}*/
		cout<<ans<<endl;
	}
	return 0;
}


赵强老师:Oracle数据库从10g到11g(16)闪回

从零开始学习Oracle数据库,从10g讲到11g;让学员读整个Oracle数据库有个全面清除的认识和学习。
  • 2018年04月07日 08:37

ACM:POJ-1979 Red And Black(JAVA的字符数组输入以及标记方法)

对于该题目直接用DFS或者BFS都可以直接暴力出来,实际上题目也不难,只需要递归一个搜索方法即可。那么用C或者C++将很容易的解决这个题目。 但是如果用JAVA写的话将存在一个昨晚让我纠结了很久的问...
  • qq_33545916
  • qq_33545916
  • 2016-04-07 10:42:15
  • 494

POJ1979 红与黑 DFS

这道题是我学了dfs之后用来强化这个思想的。就连我的水平也觉得这道题很水。 问题描述: 就像是小时候玩的吃豆子的游戏,上下左右走,然后统计一共能吃到多少个豆子。 算法...
  • Oh233
  • Oh233
  • 2014-12-24 00:34:52
  • 1476

poj1979解体报告(挑战程序设计竞赛)

打算边看这本书边做题了,看到最小生成树那里又卡住了,果然底子不够啊。 顺便写下解体报告。 poj1979要求输入2个整数,图的高度和宽度。然后输出可以移动的格子数是多少。 .代表黑格子,#代表红...
  • qq_20497909
  • qq_20497909
  • 2015-02-19 23:16:38
  • 528

poj1979 测试用例全通过,但提交就是WA,无解。

Hello,everybody,我是越爱越难,最近做POJ1979,提交时总是WA,所有的测试数据都通过了,我自己又反复看了几遍代码,感觉没毛病,扎心了!望老铁们来帮我一把!以下附上我写的Java代码...
  • the_harder_to_love
  • the_harder_to_love
  • 2017-04-02 12:37:17
  • 163

POJ--1979 Red and Black

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 28331   ...
  • Rollchuchy
  • Rollchuchy
  • 2016-02-20 20:21:20
  • 100

poj1979--DFS

深搜的水题已经不能满足我了.. //#define LOCAL #include #include #define MAXN 20 + 10 char graph[MAXN][MAXN]...
  • huzhengnan
  • huzhengnan
  • 2011-11-30 20:10:12
  • 3627

北大百练+POJ1979+DFS暴力搜索就行

点击打开链接#include #include #include #include #include #include #include #include using namespace std; c...
  • u013554860
  • u013554860
  • 2017-09-21 16:03:48
  • 113

POJ1979 HDU1312 Red and Black【DFS】

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 38045   ...
  • tigerisland45
  • tigerisland45
  • 2016-08-12 21:13:08
  • 745

多重部分和(DP):POJ1742--Coins

本文主要探究DP中经典的多重部分和问题,有很多技巧,相信你会有所收获。...
  • sonpking
  • sonpking
  • 2016-08-17 21:19:34
  • 828
收藏助手
不良信息举报
您举报文章:poj1979(回溯)
举报原因:
原因补充:

(最多只允许输入30个字)