poj 1979 dfs水题

// 练练水题,夯实基础吧

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ceil(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define gcd __gcd
#define highBit(x) (1ULL<<(63-__builtin_clzll(x)))
#define popCount __builtin_popcountll
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const long double PI = acos(-1.L);

template<class T> inline T lcm(const T& a, const T& b) { return a/gcd(a, b)*b; }
template<class T> inline T lowBit(const T& x) { return x&-x; }
template<class T> inline T maximize(T& a, const T& b) { return a=a<b?b:a; }
template<class T> inline T minimize(T& a, const T& b) { return a=a<b?a:b; }

const int maxn = 32;
char g[maxn][maxn];
int n,m;
bool vis[maxn][maxn];
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
int cnt ;
void init(){
	for (int i=0;i<n;i++)
		scanf("%s",g[i]);
	cnt = 0;
	memset(vis,0,sizeof(vis));
}

void dfs(int x,int y){
//	g[x][y] = '#';
	vis[x][y] = 1;
	//cnt = max(num,cnt);
	cnt++;
	for (int i=0;i<4;i++){
		int tx = x + dx[i];
		int ty = y + dy[i];
		if (tx>=n||tx<0||ty>=m||ty<0)
			continue;
		if (g[tx][ty]=='#')
			continue;
		if (vis[tx][ty])
			continue;
		dfs(tx,ty);
	}
}

void solve(){
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++)
			if (g[i][j] == '@')
				dfs(i,j);
	printf("%d\n",cnt);
}

int main() {
	//freopen("G:\\Code\\1.txt","r",stdin);
	while(scanf("%d%d",&m,&n)!=EOF){
		if (n==0&&m==0)
			break;
		init();
		solve();
	}
	return 0;
}

时间: 2024-11-08 21:07:15

poj 1979 dfs水题的相关文章

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

Oil Deposits(poj 1526 DFS入门题)

http://poj.org/problem?id=1562 Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12595   Accepted: 6868 Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp wor

poj 3264 RMQ 水题

题意:找到一段数字里最大值和最小值的差 水题 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 using namespace std; 8 const int maxn=550; 9 const int INF=0x3f3f3f3f; 10 in

poj 1979 Red and Black(dfs水题)

Description 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

poj 1979 dfs

水过,注意边界不能超出. #include <iostream> using namespace std; int n, m, sx, sy, dir[4][2] = {0, -1, 0, 1, 1, 0, -1, 0}, count; char diagram[23][23]; void get_diagram(void) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> diag

POJ 1000(水题)

这道题就很水啦,只要看懂题...所以我受了这个影响,再去hoj的时候想都没想就还按照这个打了,结果就像之前那篇说的那样WA. #include <stdio.h> int main() { int a,b; scanf("%d %d",&a, &b); printf("%d\n",a+b); return 0; }

CodeForces 510B DFS水题

题目大意:在图中找到一个字符可以围成一个环(至少有环四个相同元素) 题目思路:对当前点进行搜索,如果发现可以达到某个已经被查找过的点,且当前点不是由这个点而来,则查找成功. #include<cstdio> #include<stdio.h> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<cstring>

uva524(dfs水题)

题意: 把编号为1到n的珠子,串成手环,要求任意两个相邻的珠子和都为质数: 思路: 数据量只有16,打个质数表,直接dfs搜就行了: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int vis[32]; int viss[20]; int res[20],n; void init() { vis[1] = 1; for(int i = 2; i <= 31;

POJ - 3090 gcd水题

大概题意就是求\(1 \le i,j \le n\)的\(gcd(i,j) = 1\)的个数+2(对于0的特判) 正解应该是欧拉函数或者高逼格的莫比乌斯反演 但数据实在太水直接打表算了 /*H E A D*/ bool GCD[1002][1002]; inline int gcd(int a,int b){return b?gcd(b,a%b):a;} int main(){ rep(i,1,1000) rep(j,1,1000) GCD[i][j]=bool(gcd(i,j)==1); in