洛谷 P1162 填涂颜色-dfs染色法
生活随笔
收集整理的這篇文章主要介紹了
洛谷 P1162 填涂颜色-dfs染色法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
輸入:
6 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 1 1 1 0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 1輸出:
0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 2 2 1 1 1 2 2 2 1 1 2 2 2 2 1 1 1 1 1 1 1代碼如下:
#include <iostream> using namespace std; const int N = 40; int mp[N][N], mps[N][N];int n;int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};void dfs(int x, int y) {for (int i = 0; i < 4; i++) {int xx = x + dx[i], yy = y + dy[i];if (xx < 0 || xx > n + 1 || yy < 0 || yy > n + 1 || mps[xx][yy])continue;mps[xx][yy] = 1;dfs(xx, yy);} }int main() {cin >> n;for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++) {cin >> mp[i][j];mps[i][j] = mp[i][j];}dfs(0, 0);for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {if (mps[i][j])cout << mp[i][j] << " ";elsecout << 2 << " ";}cout << endl;}return 0;}總結
以上是生活随笔為你收集整理的洛谷 P1162 填涂颜色-dfs染色法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何制作手机主题
- 下一篇: 洛谷 P1122 最大子树和-求树的最大