洛谷 P1162 填塗顏色題解

題目描述

由數字00組成的方陣中,有一任意形狀閉合圈,閉合圈由數字11構成,圍圈時只走上下左右44個方向。現要求把閉合圈內的全部空間都填寫成22.例如:6 \times 66×6的方陣(n=6n=6),塗色前和塗色後的方陣以下:html

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

輸入格式

每組測試數據第一行一個整數n(1 \le n \le 30)n(1n30)ios

接下來nn行,由00和11組成的n \times nn×n的方陣。算法

方陣內只有一個閉合圈,圈內至少有一個00。數組

//感謝黃小U飲品指出本題數據和數據格式不同. 已修改(輸入格式)測試

輸出格式

已經填好數字22的完整方陣。spa

輸入輸出樣例

輸入 #1
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
輸出 #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

說明/提示

1 \le n \le 301n30code


 

題解

這道題有一個簡單的算法就是輸入時將全部爲0的數據都填寫爲2,而後從4個邊向內部進行BFS,若是搜索到2就將其改成0,並繼續搜索,若是搜索到1或0就中止搜索。orm

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <math.h>
  4 #include <algorithm>
  5 #include <string.h>
  6 
  7 using namespace std;
  8 
  9 const int MAXN = 105;
 10 int n, map[MAXN][MAXN], vis[MAXN][MAXN];
 11 int pos[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
 12 
 13 struct Node
 14 {
 15     int x, y;
 16 };
 17 
 18 Node q[MAXN];
 19 int front, rear;
 20 int a, b;
 21 
 22 void bfs()
 23 {
 24     Node now;
 25     now.x = a;
 26     now.y = b;
 27     if(map[a][b] != 2)
 28     {
 29         return;
 30     }
 31     front = rear = 0;
 32     q[rear] = now;
 33     rear++; 
 34     while(front < rear)
 35     {
 36         now = q[front++]; 
 37         if(map[now.x][now.y] == 2)
 38         {
 39             map[now.x][now.y] = 0;
 40         }
 41         if(now.x == 7)
 42         {
 43             now.x = 7;
 44         }
 45         for(int i = 0; i < 4; i++) 
 46         {
 47             int nx = now.x + pos[i][0]; 
 48             int ny = now.y + pos[i][1]; 
 49             if(nx <= n && nx > 0 && ny <= n && ny > 0  
 50                 && vis[nx][ny] == false 
 51                 && map[nx][ny] == 2) 
 52             {
 53                 map[nx][ny] = 0;
 54                 vis[nx][ny] = true;
 55                 q[rear].x = nx;
 56                 q[rear].y = ny;
 57                 rear++;
 58             }
 59         } 
 60     }
 61 }
 62 
 63 
 64 int main()
 65 {
 66     cin >> n;
 67     for(int i = 1; i <= n; i++)
 68     {
 69         for(int j = 1; j <= n; j++)
 70         {
 71             cin >> map[i][j];
 72             if(map[i][j] == 0)
 73             {
 74                 map[i][j] = 2;
 75             }
 76         }
 77     }
 78     for(int i = 1; i <= n; i++)
 79     {
 80         a = 1;
 81         b = i;
 82         bfs();
 83     }
 84     for(int i = 1; i <= n; i++)
 85     {
 86         a = n;
 87         b = i;
 88         bfs();
 89     }
 90     for(int i = 1; i <= n; i++)
 91     {
 92         a = i;
 93         b = 1;
 94         bfs();
 95     }
 96     for(int i = 1; i <= n; i++)
 97     {
 98         a = i;
 99         b = n;
100         bfs();
101     }
102     for(int i = 1; i <= n; i++)
103     {
104         for(int j = 1; j <= n; j++)
105         {
106             cout << map[i][j] << " ";
107         }
108         cout << endl;
109     }
110     return 0;
111 }

這個BFS並不難寫,不過當時犯了一個小錯誤,致使2/3/4個樣例都是WA,特別是第2個樣例在本機輸出的結果和標準答案一致,可是提交後老是說一個位置應該爲0,輸出了2。查了好久,最後發現是把pos[4][2]寫成了pos[2][4]。數組定義錯了,致使遍歷時移動的位置錯誤了,並且本機的對應內存的數據和測試機不一樣,因此在本機上是過了,可是測試機沒有過。htm

相關文章
相關標籤/搜索