nyoj1180Maze

题目链接:

http://acm.nyist.net/JudgeOnline/problem.php?pid=1180

题目大意:有个迷宫,@代表你所在位置,字符 . 表示可走,#表示不可走,问在一个地图中能到达的 . 有多少个。
值得注意的是,也要算在内。
思路:bfs,dfs应该都可以,本人用的bfs。
AC代码:

#include <stdio.h>
#include <string.h>
char a[21][21], b[21][21];
int n, m, next[444][2], con, step[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int bfs(int i) {
	if(i >= con) return 0;
	for(int j = 0; j < 4; j++) {
		int tx = next[i][0]+step[j][0], ty = next[i][1]+step[j][1];
		if(tx>=0&&tx<m&&ty>=0&&ty<n&&b[tx][ty]) {
			b[tx][ty] = 0;
			if(a[tx][ty]=='.') {
				next[con][0] = tx;
				next[con++][1] = ty;
			}
		}
	}
	int p = 1+bfs(i+1);
	return p;
}
int main() {
	int i, j;
	while(~scanf("%d%d", &n, &m)) {
		if(n==0&&m==0) break;
		memset(b, 1, sizeof(b));
		for(i = 0; i < m; i++) {getchar();
			for(j = 0; j < n; j++) {
				scanf("%c", &a[i][j]);
				if(a[i][j] == '@') {
					next[0][0] = i;
					next[0][1] = j;
				}
			}
		}
		con = 1;
		printf("%d\n", bfs(0));
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值