POJ 1979:Red and Black
来源:互联网 发布:如何网络调教sm 编辑:程序博客网 时间:2024/06/05 03:24
新博客第一篇文章,题目按照《挑战程序设计竞赛》一书来练习,希望可以有提高。
问题描述
给出有红砖与黑砖组成的图以及起点,黑砖可以走,红砖不可走,问最多可以遍历多少黑砖。
解题思路
很简单的DFS。
代码
#include <iostream>#include <stdio.h>using namespace std;char room[20][20];int w,h;int sum;void dfs(int i,int j){ sum++; room[i][j]='#'; if((i!=0)&&(room[i-1][j]=='.')) dfs(i-1,j); if((i!=(h-1))&&(room[i+1][j]=='.')) dfs(i+1,j); if((j!=0)&&(room[i][j-1]=='.')) dfs(i,j-1); if((j!=(w-1))&&(room[i][j+1]=='.')) dfs(i,j+1);}int main(){ freopen("in.txt","r",stdin); while(cin>>w>>h) { if((w==0)&&(h==0)) break; int begini,beginj; sum=0; for(int i=0;i<h;i++) for(int j=0;j<w;j++) { cin>>room[i][j]; if(room[i][j]=='@') { begini=i; beginj=j; } } dfs(begini,beginj); cout<<sum<<endl; } return 0;}
问题与反思
输入的时候w与h写反了,出了一点小bug,改完之后一次AC了。
阅读全文
0 0
- POJ 1979 Red and Black
- poj 1979 Red and Black
- POJ 1979 - Red and Black
- POJ-1979-Red and Black
- poj 1979 red and black
- poj 1979 Red and Black
- poj 1979 Red and Black
- POJ:1979 Red and Black
- POJ 1979 - Red and Black
- POJ 1979 Red and Black
- POJ 1979 Red and Black
- POJ 1979 Red and Black
- poj 1979 Red and Black
- POJ 1979 Red and Black
- poj 1979Red and Black
- POJ 1979 Red and Black
- POJ 1979 Red and Black
- POJ 1979-Red and Black
- Leetcode 540 Single Element in a Sorted Array
- EU4-14: Summarizing a meeting
- c++容器
- H
- android向web传输数据
- POJ 1979:Red and Black
- JavaScript内存泄漏
- 提交数据的三种方式
- 基于FFmpeg的开源项目small-video-record2
- hdu 2032 杨辉三角
- 屏幕适配
- Rational Krylov Method(克雷洛夫法)求解特征值问题matlab代码示例(和Arnoldi方法比较)
- 利用properties进行属性配置
- Chapter03 国际化(五) 消息格式化