怎么一直死循环
  • 板块灌水区
  • 楼主mediocre_
  • 当前回复18
  • 已保存回复18
  • 发布时间2024/10/4 14:04
  • 上次更新2024/10/4 15:41:20
查看原帖
怎么一直死循环
565707
mediocre_楼主2024/10/4 14:04
#include<bits/stdc++.h>
using namespace std;
int n, a[6][6], deep, ok;
void move(int x, int y) {
	a[x][y] = !a[x][y];
	a[x - 1][y] = !a[x - 1][y];
	a[x + 1][y] = !a[x + 1][y];
	a[x][y - 1] = !a[x][y - 1];
	a[x][y + 1] = !a[x][y + 1];
}
int h() {
	int cnt = 0;
	for (int i = 1; i <= 5; ++i)
		for (int j = 1; j <= 5; ++j)
			if (!a[i][j])
				++cnt;
	return cnt;
}
void dfs(int step, int prex, int prey) {
	if (step > deep) return ;
	if (!h()) {
		ok = 1;
		return ;
	}
	if (ok || step + h() / 5 > deep) return ;
	for (int i = 1; i <= 5; ++i)
		for (int j = 1; j <= 5; ++j) {
			if (i == prex && j == prey) continue;
			move(i, j);
			dfs(step + 1, i, j);
			move(i, j);
		}
}
int main() { 
	scanf("%d\n", &n);
	while (n--) {
		for (int i = 1; i <= 5; ++i)
			for (int j = 1; j <= 5; ++j)
				scanf("%1d", &a[i][j]);
		ok = deep = 0;
		if (!h()) {
			puts("0");
			continue;
		}
		else {
			while (++deep) {
				if (deep > 6) break;
				dfs(0, 0, 0);
				if (ok) break;
				// cout << deep << endl;
			}
				
			if (ok) 
				printf("%d", deep);
		}
		if (!ok) puts("-1");
	}
	return 0;  
}


2024/10/4 14:04
加载中...