POJ 1979 Red and Black(dfs)
来源:互联网 发布:淘宝买烟丝暗号 编辑:程序博客网 时间:2024/04/30 00:01
Description
一个n*m的矩阵,’#’不能走,’.’可以走,’@’表示起点,问最多能走多少格(一格不能走两次)
Input
多组用例,每组用例第一行为两个整数n和m表示矩阵的行列数,之后为一个n*m的矩阵,以0 0结束输入
Output
对于每组用例,输出最多能在这个矩阵上走多少格
Sample Input
Sample Output
45
59
6
13
Solution
简单dfs
Code
#include<stdio.h>#include<string.h>#define maxn 22int n,m,sx,sy,ans;char map[maxn][maxn]; int dx[]={-1,0,1,0};int dy[]={0,-1,0,1};void dfs(int x,int y){ for(int i=0;i<4;i++) { int xx=x+dx[i],yy=y+dy[i]; if(xx<0||xx>=n||yy<0||yy>=m||map[xx][yy]!='.')continue; ans++; map[xx][yy]='#'; dfs(xx,yy); }}int main(){ while(~scanf("%d%d",&m,&n),n||m) { ans=1; for(int i=0;i<n;i++) { scanf("%s",map[i]); for(int j=0;j<m;j++) if(map[i][j]=='@') sx=i,sy=j,map[i][j]='#'; } dfs(sx,sy); printf("%d\n",ans); } return 0;}
0 0
- POJ 1979 Red and Black(DFS)
- poj 1979 Red and Black (DFS)
- POJ 1979 Red and Black (DFS)
- POJ 1979 Red and Black (DFS)
- POJ 1979 Red and Black (dfs)
- POJ 1979 Red and Black (DFS)
- POJ 1979 Red and Black(dfs)
- POJ 1979-Red and Black(DFS)
- POJ 1979:Red and Black(dfs)
- poj 1979 Red and Black(DFS)
- POJ 1979 Red and Black(DFS)
- 【POJ】-1979-Red and Black(DFS)
- POJ-1979-Red and Black(DFS)
- poj 1979Red and Black(BFS DFS)
- poj 1979 Red and Black ---DFS
- POJ 1979 Red and Black (DFS)
- poj 1979 Red and Black (DFS)
- poj 1979 Red and Black(DFS)
- PetSc学习记录
- [LeetCode]Binary Tree Level Order Traversal II
- 使用Git开发(1)
- 论sql注入的攻与防
- 【Android实测】eclipse run as timeout的解决办法
- POJ 1979 Red and Black(dfs)
- 内存那些事
- KVM error when create guest
- 什么是 IPv4 多播?
- android edittext只能输入字母和数字,默认弹出英文输入法 。
- 完全详解--使用Resource实现多语言的支持
- 我的javal旅程开启之路
- Android SharedPreferences使用以及原理详解
- 几款自然小清新风格的艺术涂料推荐