题意:

      给你一个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. HDOJ-1540(线段树+较复杂的单点修改和区间查询)

    Tunnel Warfare HDOJ-1540 这题关于线段树的操作有一定的难度,需要较好的思维能力. 关于题目的详细解答已经在代码中体现了. #include<iostream> #i ...

  2. Hadoop的常用命令

    注:hadoop的使用命令包含 hadoop fs 开头 or hdfs dfs开头 等多种形式来操作. 这里以hadoo fs的形式来介绍在这些命令 1.列出根目录下所有的目录或文件 hadoop ...

  3. Elasticsearch核心技术(一):Elasticsearch环境搭建

    磨刀不误砍柴工,要学习Elasticsearch,首先要搭建起来一套学习环境,本文为手把手教你在MacOS上面搭建Elasticsearch学习环境. 1.1 Elasticsearch安装 Elas ...

  4. BurpSuite 一些小技巧

    拦截请求 设置不拦截下面的网站(记得保存项目,不然每次都要添加) .*\.?baidu.com|.*\.?google.com|.*\.?bcebos.com|.*\.?bdstatic.com|.* ...

  5. ThinVnc-身份验证绕过(CVE-2019-17662)

    ThinVnc-身份验证绕过(CVE-2019-17662) 简介 ThinVNC是一款以网页浏览器为基础设计的远端桌面连接工具,不局限用户端使用那种作业平台,都可以通过采用HTML5为标准的浏览器来 ...

  6. MyBatis架构分析

      我们都知道Mybatis是一个非常小巧灵活的ORM框架,深受国内广大开发者的喜爱,我们知道它的出现某种程度是为了消除所有的JDBC代码和参数的手工设置以及结果集的封装问题:基于这个一点,我们就可以 ...

  7. 自己挖的坑自己填--Mybatis mapper文件if标签中number类型及String类型的坑

    1.现象描述 (1)使用 Mybatis 在进行数据更新时,大部分时候update语句都需要通过动态SQL进行拼接.在其中,if标签中经常会有 xxx !='' 这种判断,若 number 类型的字段 ...

  8. HDU_6693 Valentine's Day 【概率问题】

    一.题目 Valentine's Day 二.分析 假设$ s_0 $代表不开心的概率,$ s_1 $代表开心一次的概率. 那么随便取一个物品,那么它的开心概率为$ p _i $,可以推导加入之后使女 ...

  9. javascript中的Strict模式

    目录 简介 使用Strict mode strict mode的新特性 强制抛出异常 简化变量的使用 简化arguments 让javascript变得更加安全 保留关键字和function的位置 总 ...

  10. 一次线上MySQL主从延迟排查

    今天早上来上班,发现zabbix一直告警主从延迟,mysql slave Seconds_Behind_Master (mysql.slave_status[Seconds_Behind_Master ...