山峰和山谷 Ridges and Valleys

真的觉得好沧桑~(╯‵□′)╯︵┻━┻

什么辣鸡玩意儿?当我快乐地打dfs时50分时,别人都用bfs愉快地A了。

放几个数据:

3
1 1 1
1 1 1
1 1 1
ans:1 1


4
1 1 1 3
1 1 3 3
1 1 3 3
1 1 3 3

我的lj算法在某些数被同类包围时会错,因为此时的tot0和tot1都是0。

改进一下就好了。拜拜~

#include<cstdio>
#include<cctype>
#include<iostream>
using namespace std;

const int N = 1002;
int Max, Min, ans1, ans0, n, G[N][N], dir[10][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}, vis[N][N];

void read(int &x) {
	x = 0;
	char s = getchar();
	while(! isdigit(s))
		s = getchar();
	while(isdigit(s)) {
		x = (x << 1) + (x << 3) + (s ^ 48);
		s = getchar();
	}
} 

int getSta(const int x, const int y) {
	int tot1 = 0, tot0 = 0;
	for(int i = 0; i < 8; ++ i) {
		int tx = x + dir[i][0];
		int ty = y + dir[i][1];
		if(tx < 1 || tx > n || ty < 1 || ty > n)
			continue;
		if(vis[tx][ty] == 2 && G[tx][ty] == G[x][y])
			return -1;
		if(G[tx][ty] > G[x][y])
			++ tot0;
		if(G[tx][ty] < G[x][y])
			++ tot1;
	}
	if(tot0 && tot1)
		return -1;
	if(tot0)
		return 0;
	if(tot1)//之前写法:else return 1;
		return 1;
	return 2;
}

bool dfs(const int x, const int y, const int sta, const int cnt) {
	int S = getSta(x, y);
	bool flag = 0;
	if(S == -1 || (sta != S && sta != -1 && S != 2)) {
		vis[x][y] = 2;
		return 0;
	}
	for(int i = 0; i < 8; ++ i) {
		int tx = x + dir[i][0];
		int ty = y + dir[i][1];
		if(tx < 1 || tx > n || ty < 1 || ty > n || vis[tx][ty] || G[tx][ty] != G[x][y])
			continue;
		vis[tx][ty] = 1;
		if(dfs(tx, ty, S, cnt + 1) == 0)
			flag = 1;
	}
	if(flag) {
		vis[x][y] = 2;
		return 0;
	}
	return 1;
}

int main() {
	int sta;
	read(n);
	Min = (1 << 30) - 1;
	for(int i = 1; i <= n; ++ i)
		for(int j = 1; j <= n; ++ j) {
			read(G[i][j]);
			Max = max(Max, G[i][j]);
			Min = min(Min, G[i][j]);
		}
	if(Max == Min)
		return ! printf("1 1\n");
	for(int i = 1; i <= n; ++ i) {
		for(int j = 1; j <= n; ++ j) {
			if(vis[i][j])
				continue;
			if(dfs(i, j, -1, 1)) {
				sta = getSta(i, j);
				if(sta == 1)
					++ ans1;
				else
					++ ans0;
			}
		}
	}
	printf("%d %d\n", ans1, ans0);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值