admin 管理员组

文章数量: 893601

洛谷 P1162填图颜色

洛谷 P1162填图颜色

题目链接

#include <bits/stdc++.h>
using namespace std;
int xx[] = {0, -1, 0, 1};
int yy[] = {1, 0, -1, 0};
int mp[40][40];
bool vis[40][40];
int n;int main()
{scanf("%d", &n);for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){scanf("%d", &mp[i][j]);}}printf("\n\n");queue<int> x;queue<int> y;x.push(0);y.push(0);vis[0][0] = 1;while (!x.empty()){for (int i = 0; i < 4; i++){int dx = x.front() + xx[i];int dy = y.front() + yy[i];if (dx >= 0 && dx <= n + 1 && dy >= 0 && dy <= n + 1 && mp[dx][dy] == 0 && !vis[dx][dy]){x.push(dx);y.push(dy);vis[dx][dy] = 1;}}x.pop();y.pop();}for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){if (mp[i][j] == 0 && vis[i][j] == 0){cout << 2;}else{cout << mp[i][j];}cout << " ";}cout << endl;}return 0;
} 

本文标签: 洛谷 P1162填图颜色