惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
Spread Privacy
Spread Privacy
IT之家
IT之家
F
Fortinet All Blogs
GbyAI
GbyAI
T
Threat Research - Cisco Blogs
月光博客
月光博客
WordPress大学
WordPress大学
V
Vulnerabilities – Threatpost
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
MyScale Blog
MyScale Blog
小众软件
小众软件
AI
AI
云风的 BLOG
云风的 BLOG
Jina AI
Jina AI
博客园 - 【当耐特】
T
Tailwind CSS Blog
Recorded Future
Recorded Future
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
Schneier on Security
Schneier on Security
Engineering at Meta
Engineering at Meta
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
PCI Perspectives
PCI Perspectives
K
Kaspersky official blog
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
C
CERT Recently Published Vulnerability Notes
Security Latest
Security Latest
爱范儿
爱范儿
L
LangChain Blog
V
Visual Studio Blog
W
WeLiveSecurity
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
Cisco Blogs
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - zealsoft

洛谷 P1816 忠诚题解 洛谷 P2384 最短路题解 洛谷 P2725 邮票题解 洛谷 P2722 总分题解 洛谷 P1219 八皇后题解 洛谷 P2921 在农场万圣节Trick or Treat on the Farm题解 洛谷 P1330 封锁阳光大学题解 洛谷 P1032 字串变换题解 洛谷 P1443 马的遍历题解 洛谷 P1280 尼克的任务题解 洛谷 P1020导弹拦截题解 洛谷 P1141 01迷宫题解 VisualSVNServer无法卸载也无法安装,报告不是有效的MOF文件(0x8004401e)错误 视频捕捉的格式问题 一个VxWorks源代码网站 找个轻量级的Log库还挺难 TAU G2中的BitString和OctetString W32.Downadup.autorun病毒的清除 如何用Visual Studio 2005编译Wireshark的插件
洛谷 P1162 填涂颜色题解
zealsoft · 2019-08-24 · via 博客园 - zealsoft

题目描述

由数字00组成的方阵中,有一任意形状闭合圈,闭合圈由数字11构成,围圈时只走上下左右44个方向。现要求把闭合圈内的所有空间都填写成22.例如:6 \times 66×6的方阵(n=6n=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

输入格式

每组测试数据第一行一个整数n(1 \le n \le 30)n(1n30)

接下来nn行,由00和11组成的n \times nn×n的方阵。

方阵内只有一个闭合圈,圈内至少有一个00。

//感谢黄小U饮品指出本题数据和数据格式不一样. 已修改(输入格式)

输出格式

已经填好数字22的完整方阵。

输入输出样例

输入 #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 301n30


题解

这道题有一个简单的算法就是输入时将所有为0的数据都填写为2,然后从4个边向内部进行BFS,如果搜索到2就将其改为0,并继续搜索,如果搜索到1或0就停止搜索。

  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]。数组定义错了,导致遍历时移动的位置错误了,而且本机的对应内存的数据和测试机不同,所以在本机上是过了,但是测试机没有过。