题意:

      给你一个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 最短路(想法好题目)的更多相关文章

  1. 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 ...

  2. Zoj 3781(构造)

    Zoj 3781(构造) Zoj 3781 As we all know, Coach Gao is a talented chef, because he is able to cook M dis ...

  3. ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...

  4. 【最短路+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 ...

  5. 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 ...

  6. 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 ...

  7. ZOJ 3781 Paint the Grid Reloaded 连通块

    LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...

  8. ZOJ - 3781 Paint the Grid Reloaded 题解

    题目大意: 给一个n*m的X O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X—>O或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色. 思路: 1. ...

  9. Paint the Grid Reloaded ZOJ - 3781 图论变形

    Paint the Grid Reloaded Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %ll ...

随机推荐

  1. DatePicker 多时间粒度选择器组件

    使用方法: 在.vue文件引入 import ruiDatePicker from '@/components/rattenking-dtpicker/rattenking-dtpicker.vue' ...

  2. 蓝桥杯-分考场(dfs)

    分考场 PREV-53 这题的解决方法使用dfs,因为数据很小,才100. 每次当前的人人是否可以和前面的组队,设置两个数组group和fri /*DFS求解:思路每次判断输入的人是否可以和前面的组队 ...

  3. List调用toString()方法后,去除两头的中括号

    import org.apache.commons.lang.StringUtils; public class Test {    public static void main(String[] ...

  4. ubuntu18.04+gunicorn+nginx+supervisor+mysql+redis安装django项目

    Ubuntu18.04 install Django project 项目准备: ECS 实例 (云服务器) 此安装部署方案适合本地ubuntu18.04系统安装和虚拟机中ubuntu18.04系统安 ...

  5. 60秒定位问题,十倍程序员的Debug日常

    作者:陶建辉 这是我在 2020 年 5 月写的一篇内部博客,当时是希望研发和技术支持同学能够帮助用户快速定位 Bug,解决问题.2020 年 12 月我又迭代了一版,并还针对此进行了内部的培训.这段 ...

  6. myeclipse js报错

    Myeclipse 版本10.1 加载的js报错,解决方法: window -> preferences -> myeclipse -> validation,在右边下拉框找到 Ja ...

  7. 基于renren-fast的快速入门项目实战(实现报表增删改查)

    基于renren-fast的快速入门项目实战(实现报表增删改查) 说明:renren-fast是一个开源的基于springboot的前后端分离手脚架,当前版本是3.0 官方开发文档需付费,对于新手而言 ...

  8. 让JS代码Level提升的忍者秘籍(实用)

    本文章共2377字,预计阅读时间5-10分钟. 前言 没有前言. 你准备好成为同事眼中深藏不露.高深莫测.阳光帅气的前端开发了吗? 那就开始吧! 本文秉承宗旨:代码实用与逼格并存. 提升JS代码Lev ...

  9. 在swoole中制作一款仿制laravel的框架

    首先需要确定一下思路:我希望基于swoole的扩展开发的代码在run起来的时候,在接收到ws或是tcp等消息时,自动路由到某个类上,同时类可以实现加载类的依赖注入功能.目前市面上占据主流的一款框架La ...

  10. python函数之有参装饰器

    一.为什么要有有参装饰器? 来看之前的无参装饰器 # 无参装饰器 def outter(func): def wrapper(*args,**kwargs): start = time.time() ...