其他分享
首页 > 其他分享> > P1162 填涂颜色(DFS 解题方式新颖

P1162 填涂颜色(DFS 解题方式新颖

作者:互联网

题目链接https://www.luogu.com.cn/problem/P1162
//一开始想找如何判断0在1的封闭曲线内,后来发现只要找在1之外的就可以
//而在1之外的点,一般都可以通过不断扩展最后与边缘相接
//所以只要dfs四个边上为0的点,不断往里找0,并把其的vis赋值为1
//最后vis既不是1,而且图a中那个点也不是1,则为2;
#include <iostream> #include <set> #include <vector> #include <cmath> #include <cstdio> #include <cstring> #include <algorithm> #define ll long long using namespace std; const int M = 1e2 + 1; int a[M][M];//存图 int b[M][M];//答案的图 int vis[M][M];//存点的使用情况 int tx[] = { 0,1,0,-1 }; int ty[] = { 1,0,-1,0 }; int n; bool check(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= n; } inline void dfs(int x, int y) { if (a[x][y] == 0) { vis[x][y] = 1; for (int i =0 ; i < 4; i++) { int dx = x + tx[i]; int dy = y + ty[i]; if (check(dx, dy) && a[dx][dy] == 0&&vis[dx][dy]!=1) { dfs(dx, dy); } } } return ; } int main() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; b[i][j] = a[i][j]; } } for (int i = 1; i <= n; i++) { dfs(i, 1); dfs(i, n); } for (int i = 1; i <= n; i++) { dfs(1, i); dfs(n, i); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (vis[i][j] == 0 && a[i][j] != 1) { b[i][j] = 2; } cout << b[i][j] << " "; } cout << endl; } }

 

标签:填涂,int,DFS,vis,long,&&,P1162,include
来源: https://www.cnblogs.com/BlogBaudelaire/p/14604856.html