poj 1979 , zoj 2165 Red and Black【dfs,入门题】

题目连接: http://poj.org/problem?id=1979

大致题意:

给出两个数m和n,代表n行m列,都不超过20,然后是n行m列的图,包括'.' , '#' , '@'3个字符。

@代表你的位置,'.'代表通路,‘#’代表墙,问你最远不回头能走几个‘.'  。‘@’算一个。

输入0 0 结束。

题意本不是这样,被我该了。。。意思不变

#include<stdio.h>
#include<string.h>

char map[22][22];
int m, n, ans;
int dir[4][2] = 
{
	{1, 0}, {0, 1}, {-1, 0}, {0, -1}
};

void dfs(int x, int y)
{
	map[x][y] = '#';
	for(int i = 0; i < 4; i++) {
		int a = x + dir[i][0];
		int b = y + dir[i][1];
		if(a >= 0 && a < n && b >= 0 && b < m && map[a][b] == '.') {
			dfs(a, b);
			ans++;
		}
	}
//	map[x][y] = '.';
}

int main()
{
	while(scanf("%d %d%*c", &m, &n), m+n) {
		int x, y;
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < m; j++) {
				scanf("%c", &map[i][j]);
				if(map[i][j] == '@') {
					x = i;
					y = j;
				}
			}
			scanf("%*c");
		}
		ans = 1;
		dfs(x, y);
		printf("%d\n", ans);
	}
	return 0;
}

  

posted @ 2012-11-03 18:25  小猴子、  阅读(517)  评论(0编辑  收藏  举报