ZOJ 3781 最短路(想法好题目)
题意:
给你一个n*m的矩阵,上面只有两种字符,X或者O,每次可以同时改变相同颜色的一个连通块,上下左右连通才算连通,用最小的步数把这个图弄成全是X或者全是O,题意要是没看懂看下面的样例。
Sample Input
2
2 2
OX
OX
3 3
XOX
OXO
XOX
Sample Output
1
2
Hint
For the second sample, one optimal solution is:
Step 1. flip (2, 2)
XOX
OOO
XOX
Step 2. flip (1, 2)
XXX
XXX
XXX
思路:
这个可以用最短路来做(也可以直接广搜,因为是在格子上找距离),首先我们要把每一个连通块缩成一个点,然后把相邻的联通快之间建边,然后枚举每一个点为起点,跑单源最短路,然后对于每一个点的当前答案就是所有点中离他最远的那个,最后在在每一个最远的点钟找到最小的那个,为什么这么做是对的,我们就假设我们枚举的最短路的起点是中心点,我们从中心点往外扩,每一次绝对可以扩展一层,这
一层有两种扩展方式,改变中间或者改变要扩展的这层,画一下就理解了。。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<map> #define N_node 1600 + 100
#define N_edge 2000000
#define INF 100000000 using namespace std; typedef struct
{
int to ,next ,cost;
}STAR; STAR E[N_edge];
int list[N_node] ,tot;
int s_x[N_node];
int mapp[50][50] ,n ,m;
int mk[50][50];
int dir[4][2] = {0 ,1 ,0 ,-1 ,1 ,0 ,-1 ,0};
map<int ,map<int ,int> >kk; void add(int a ,int b ,int c)
{
E[++tot].to = b;
E[tot].cost = c;
E[tot].next = list[a];
list[a] = tot;
} int spfa(int s ,int n)
{
for(int i = 0 ;i <= n ;i ++)
s_x[i] = INF;
int mark[N_node] = {0};
mark[s] = 1;
s_x[s] = 0;
queue<int>q;
q.push(s);
while(!q.empty())
{
int xin ,tou;
tou = q.front();
q.pop();
mark[tou] = 0;
for(int k = list[tou] ;k ;k = E[k].next)
{
xin = E[k].to;
if(s_x[xin] > s_x[tou] + E[k].cost)
{
s_x[xin] = s_x[tou] + E[k].cost;
if(!mark[xin])
{
mark[xin] = 1;
q.push(xin);
}
}
}
}
int now = 0;
for(int i = 1 ;i <= n ;i ++)
if(now < s_x[i]) now = s_x[i];
return now;
} bool ok(int x ,int y ,int ys)
{
return x >= 1 && x <= n && y >= 1
&& y <= m && !mk[x][y]
&& mapp[x][y] == ys; } void DFS(int x ,int y ,int now ,int ys)
{
for(int i = 0 ;i < 4 ;i ++)
{
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if(ok(xx ,yy ,ys))
{
mk[xx][yy] = now;
DFS(xx ,yy ,now ,ys);
}
}
return ;
} int main ()
{
int i ,j ,t;
char str[50];
scanf("%d" ,&t);
while(t --)
{
scanf("%d %d" ,&n ,&m);
for(i = 1 ;i <= n ;i ++)
{
scanf("%s" ,str);
for(j = 1 ;j <= m ;j ++)
mapp[i][j] = str[j-1] == 'X';
}
memset(mk ,0 ,sizeof(mk));
int now = 0;
for(i = 1 ;i <= n ;i ++)
for(j = 1 ;j <= m ;j ++)
{
if(mk[i][j]) continue;
mk[i][j] = ++now;
DFS(i ,j ,now ,mapp[i][j]);
}
kk.clear();
memset(list ,0 ,sizeof(list)) ,tot = 1;
for(i = 1 ;i <= n ;i ++)
for(j = 1 ;j <= m ;j ++)
{
if(i > 1)
{
if(mk[i][j] != mk[i-1][j] && !kk[mk[i][j]][mk[i-1][j]])
{
kk[mk[i][j]][mk[i-1][j]] = 1;
add(mk[i][j] ,mk[i-1][j] ,1);
}
}
if(i < n)
{
if(mk[i][j] != mk[i+1][j] && !kk[mk[i][j]][mk[i+1][j]])
{
kk[mk[i][j]][mk[i+1][j]] = 1;
add(mk[i][j] ,mk[i+1][j] ,1);
}
}
if(j > 1)
{
if(mk[i][j] != mk[i][j-1] && !kk[mk[i][j]][mk[i][j-1]])
{
kk[mk[i][j]][mk[i][j-1]] = 1;
add(mk[i][j] ,mk[i][j-1] ,1);
}
}
if(j < m)
{
if(mk[i][j] != mk[i][j+1] && !kk[mk[i][j]][mk[i][j+1]])
{
kk[mk[i][j]][mk[i][j+1]] = 1;
add(mk[i][j] ,mk[i][j+1] ,1);
}
}
}
int ans = INF;
for(i = 1 ;i <= now ;i ++)
{
int tmp = spfa(i ,now);
if(ans > tmp) ans = tmp;
}
printf("%d\n" ,ans);
}
return 0;
}
ZOJ 3781 最短路(想法好题目)的更多相关文章
- ZOJ 3781 Paint the Grid Reloaded(BFS+缩点思想)
Paint the Grid Reloaded Time Limit: 2 Seconds Memory Limit: 65536 KB Leo has a grid with N rows ...
- Zoj 3781(构造)
Zoj 3781(构造) Zoj 3781 As we all know, Coach Gao is a talented chef, because he is able to cook M dis ...
- ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...
- 【最短路+bfs+缩点】Paint the Grid Reloaded ZOJ - 3781
题目: Leo has a grid with N rows and M columns. All cells are painted with either black or white initi ...
- ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds Me ...
- ZOJ 3781 Paint the Grid Reloaded(BFS)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Leo has a grid with N rows an ...
- ZOJ 3781 Paint the Grid Reloaded 连通块
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...
- ZOJ - 3781 Paint the Grid Reloaded 题解
题目大意: 给一个n*m的X O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X—>O或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色. 思路: 1. ...
- Paint the Grid Reloaded ZOJ - 3781 图论变形
Paint the Grid Reloaded Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %ll ...
随机推荐
- 微信小程序弹出框滚动穿透问题
1.在你的遮罩层最外层加 catchtouchmove="noneEnoughPeople" 里面是你的方法名2.noneEnoughPeople: function () { c ...
- 使用函数式语言实践DDD
长期以来我都在实践OOP,进而通过OOP来实现DDD,特别是如何通过面向对象的技巧来建立一个领域模型.OO的一些特性在建立领域模型时显得恰如其分,能否掌握OO的技巧,对创建领域模型有着至关重要的作用. ...
- js输入框只能输入数字
1.只允许输入数字 <input type="text" onkeyup="this.value=this.value.replace(/\D/g,'')&quo ...
- ASP.NET .Core 集成 React SPA 应用
AgileConfig的UI使用react重写快完成了.上次搞定了基于jwt的登录模式(AntDesign Pro + .NET Core 实现基于JWT的登录认证),但是还有点问题.现在使用reac ...
- 七种join的书写规范
在mysql中的两表进行连接时,总共有7种连接情况,具体可见下图 由图的从左到右的顺序 图1.左连接(left join):返回左表中的所有记录和右表中的连接字符字段相等的记录,若右表没有匹配值则补N ...
- Java 8 Stream API 详解
Java 8 中的 Stream 是对集合(Collection)对象功能的增强,它专注于对集合对象进行各种非常便利.高效的聚合操作(aggregate operation),或者大批量数据操作 (b ...
- 【安全研究】Domain fronting域名前置网络攻击技术
出品|MS08067实验室(www.ms08067.com) 千里百科 Domain Fronting基于HTTPS通用规避技术,也被称为域前端网络攻击技术.这是一种用来隐藏Metasploit,Co ...
- Android Studio 之创建自定义控件
•前言 常用控件和布局的继承结构,如下图所示: 可以看到,我们所用的所有的控件都是直接或者间接的继承自View的: 所用的所有布局都是直接或者间接继承自ViewGroup的: View 是 Andro ...
- 前端学习 node 快速入门 系列 —— 报名系统 - [express]
其他章节请看: 前端学习 node 快速入门 系列 报名系统 - [express] 最简单的报名系统: 只有两个页面 人员信息列表页:展示已报名的人员信息列表.里面有一个报名按钮,点击按钮则会跳转到 ...
- 各种OJ网站,刷题必备
各种OJ网站 落谷 vijos JoyOI CodeVS Comet OJ 北京大学 浙江大学 杭州电子科技大学 信息学奥赛一本通 以上就是本蒟蒻所知的OJ网站 收集和打字应该值一个赞吧