Red and Black (POJ 1979,深度优先搜索)
来源:互联网 发布:ip地址显示杭州阿里云 编辑:程序博客网 时间:2024/06/06 03:13
题目链接
这道题和前面的例题Lake Couting十分相似。只需判断四个方向上是否为'.',若为'.',移动过去并将这个'.'改为'#',移动的次数加一即为答案。本题比较简单,就不多说了。
#include <cstdio>const int MAX_W = 20;const int MAX_H = 20;int W, H;char t[MAX_W][MAX_H];int dx[4] = {-1,1,0,0};int dy[4] = {0,0,1,-1};int res;void dfs(int i,int j)//对四个方向判断,若为'.',移动,计数器+1,并将'.'改为'#' {for(int k=0; k<4; k++){int y = i+dx[k];int x = j+dy[k]; if(0<=x && x<W && 0<=y && y<H && t[y][x] == '.'){res++;t[y][x] = '#';dfs(i+dx[k], j+dy[k]);}}return ;}int main(){while(scanf("%d%d", &W, &H)==2 && W!=0){res = 1;for(int i=0; i<H; i++){for(int j=0; j<W; j++){scanf(" %c", &t[i][j]);}}for(int i=0; i<H; i++){for(int j=0; j<W; j++){if(t[i][j] == '@'){dfs(i, j);break;}}}printf("%d\n",res);} return 0;}
阅读全文
0 0
- POJ 1979 Red and Black【深度优先搜索】
- Red and Black (POJ 1979,深度优先搜索)
- POJ 1979 Red and Black (深度优先遍历)
- POJ 1979 Red and Black(BFS广度优先搜索)
- red and black (深度优先搜索算法dfs)
- poj1979 Red and Black DFS深度优先搜索+剪枝
- 深度优先搜索(DFS):POJ1979--Red and Black
- POJ1979(DFS深度优先搜索)之 Red and Black
- Red and Black (深度优先搜索 DFS)
- POJ 1979 Red and Black DFS搜索
- POJ 1979 Red and Black(搜索)
- POJ 1979 :Red and Black 搜索
- HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)
- Red and Black---DFS深度优先算法
- Hdu1312 - Red and Black - 广度优先搜索
- POJ1979:Red and Black(广度优先搜索)
- HDu 1312深度搜索---Red and Black
- POJ1979 /Openjudge1818 Red and Black解题报告(深度优先搜索,图的遍历)
- ABP官方文档(十七)【仓储】
- Java并发编程:Java内存模型和volatile关键字解析
- python (一)
- Apache-Ant
- windows Server 2008 r2部署JSP项目
- Red and Black (POJ 1979,深度优先搜索)
- larvael学习之Eloquent
- Thinkphp 结合kibana 来监测设备(监测车辆情况的设备)是否还在正常运行
- C#中的GUID生成格式
- SpringBoot拦截器或过滤器中使用流读取参数后,controller中注解读取不到参数
- 广度、深度、易用性,详解6大机器学习云
- matlab中控制并口打标Failure to find requested data acquisition device: parallel.解决方案
- 三阶幻方
- ubuntu 安装搜狗输入法后,皮肤错乱解决办法【ubuntu16.04 x64】