博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 525D Arthur and Walls
阅读量:4321 次
发布时间:2019-06-06

本文共 2203 字,大约阅读时间需要 7 分钟。

题意:给你一个矩阵 只含有 '*' 和 '.',问你使得所有的'.' 的联通块都是矩形要删除最少的'*'.问你要删多少个。

解题思路:搜索,这题和515D类似,都不是直接去找答案,而是根据性质去找  我们知道,有一个2×2的区域,只有一个点是'*',这个点就会变成‘.‘,所以可以利用这个性质进行广搜。

解题代码:

1 // File Name: d.cpp  2 // Author: darkdream  3 // Created Time: 2015年03月27日 星期五 16时05分31秒  4   5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 #include
21 #define LL long long 22 23 using namespace std; 24 int n ,m ; 25 char mp[2005][2005]; 26 bool vis[2005][2005]; 27 struct node{ 28 int x, y; 29 node(){} 30 node(int _x, int _y) 31 { 32 x = _x; 33 y = _y ; 34 } 35 }tmp; 36 int lx, ly ,rx,ry; 37 int xadd[] = { 0,0,1,-1}; 38 int yadd[] = { 1,-1,0,0}; 39 queue
qu; 40 void solve(int tx,int ty) 41 { 42 mp[tx][ty] = '.'; 43 qu.push(node(tx,ty)); 44 for(int i = 0 ;i <= 3 ;i ++) 45 qu.push(node(tx+xadd[i],ty+yadd[i])) ; 46 } 47 void bfs(int x,int y) 48 { 49 qu.push(node(x,y)); 50 int tx,ty; 51 while(!qu.empty()) 52 { 53 tmp = qu.front(); 54 qu.pop(); 55 if(mp[tmp.x][tmp.y] != '.') 56 continue; 57 if(mp[tmp.x-1][tmp.y] == '.' && mp[tmp.x][tmp.y+1] =='.' && mp[tmp.x-1][tmp.y+1] == '*') 58 { 59 tx = tmp.x -1; 60 ty = tmp.y +1; 61 solve(tx,ty); 62 } 63 if(mp[tmp.x-1][tmp.y] == '.' && mp[tmp.x][tmp.y-1] =='.' && mp[tmp.x-1][tmp.y-1] == '*') 64 { 65 tx = tmp.x -1; 66 ty = tmp.y -1; 67 solve(tx,ty); 68 } 69 if(mp[tmp.x+1][tmp.y] == '.' && mp[tmp.x][tmp.y+1] =='.' && mp[tmp.x+1][tmp.y+1] == '*') 70 { 71 tx = tmp.x +1; 72 ty = tmp.y +1; 73 solve(tx,ty); 74 } 75 if(mp[tmp.x+1][tmp.y] == '.' && mp[tmp.x][tmp.y-1] =='.' && mp[tmp.x+1][tmp.y-1] == '*') 76 { 77 tx = tmp.x +1; 78 ty = tmp.y -1; 79 solve(tx,ty); 80 } 81 } 82 } 83 int main(){ 84 scanf("%d %d",&n,&m); 85 for(int i = 1;i <= n;i ++) 86 { 87 scanf("%s",&mp[i][1]); 88 } 89 for(int i = 1 ;i <= n;i ++) 90 { 91 for(int j = 1;j <= m;j ++) 92 { 93 if( mp[i][j] == '.') 94 { 95 bfs(i,j); 96 } 97 } 98 } 99 for(int i = 1;i <= n;i ++)100 puts(&mp[i][1]);101 return 0;102 }
View Code

 

转载于:https://www.cnblogs.com/zyue/p/4372838.html

你可能感兴趣的文章
Windows 7 Ultimate(旗舰版)SP1 32/64位官方原版下载(2011年5月12日更新版)
查看>>
javascript操作cookie
查看>>
深入理解HTTP协议(转)
查看>>
NHibernate讲解
查看>>
剑指offer-二叉树中和为某一值的路径
查看>>
spark算子
查看>>
(转)Linux服务器SNMP常用OID
查看>>
USB各种模式 解释
查看>>
数据访问-----ADO.NET 小结和练习
查看>>
Linux lsof详解
查看>>
子组件给父组件传数据
查看>>
unix/linux下的共享内存、信号量、队列信息管理
查看>>
Hilbert先生旅馆的故事
查看>>
采访吴岳师兄有感 by 王宇飞
查看>>
LVS简略介绍
查看>>
hdu 1021 Fibonacci Again
查看>>
JVM架构_XmnXmsXmxXss有什么区别:转
查看>>
PHPExcel 使用心得
查看>>
洛谷 P3374 【模板】树状数组 1(单点加,区间和)
查看>>
verilog 代码编写小记
查看>>