【OJ】Red and black poj1979 // acmclub.com 6659

简介: #include#includeusing namespace std;char tiles[21][21];int x,y,w,h,ans;int dx[4]={-1,0,0,1},dy[4]={0,-1,1,0};//^void dfs(int sx,i...
#include<iostream>
#include<cstdio>
using namespace std;
char tiles[21][21];
int x,y,w,h,ans;
int dx[4]={-1,0,0,1},dy[4]={0,-1,1,0};//^<>
void dfs(int sx,int sy){
	ans++;tiles[sy][sx]='#';
	for(int i=0;i<4;i++){
		int nx=sx+dx[i],ny=sy+dy[i];
		if(nx>=1&&nx<=w&&ny>=1&&ny<=h&&tiles[ny][nx]=='.'/**/)dfs(nx,ny); 
	}
}
int main(){
//	freopen("red.txt","r",stdin);
	while(cin>>w>>h&&(w||h)){
		 ans=0;		getchar();
		for(int i=1;i<=h;i++){
	 		for(int j=1;j<=w;j++){
 				tiles[i][j]=getchar();
 				if(tiles[i][j]=='@'){x=j;y=i;}
 			}
	   		getchar();
		}
		dfs(x,y);
		cout<<ans<<endl;			
	}
	
	return 0;
}

目录
相关文章
|
9月前
HDU - 1312 Red and Black(DFS)
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles. Write a program to count the number of black
57 0
|
10月前
|
C++
【PAT甲级 - C++题解】1135 Is It A Red-Black Tree
【PAT甲级 - C++题解】1135 Is It A Red-Black Tree
62 0
|
机器学习/深度学习 Windows
Codeforces Round #748 (Div. 3) F - Red-Black Number (记忆化搜索)
Codeforces Round #748 (Div. 3) F - Red-Black Number (记忆化搜索)
77 0
CodeForces - 1469B Red and Blue (前缀和)
CodeForces - 1469B Red and Blue (前缀和)
74 0
hdu 1312 Red and Black(BFS)
hdu 1312 Red and Black(BFS)
118 0
hdu 1312 Red and Black
一个人从@点出发,求他所能到达的'.'的数目,'#'不可走,@本身算1个点。 思路:搜索入门题。
123 0
HDOJ 1312 (POJ 1979) Red and Black
HDOJ 1312 (POJ 1979) Red and Black
90 0
ZOJ1067 Color Me Less
复制代码#include <iostream> #include <cmath> #include <limits> using namespace std; const int MAXSIZE = 100; int pos[100];//记录对应的最小值所在位置 struct RGB {//颜.
1433 0