hdu5652 India and China Origins BFS+二分 或 二维的并查集

国央企创新负责人如何通过平台实现科研成果的快速识别与转化?.docx 科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。 立即下载

India and China Origins

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1165    Accepted Submission(s): 382


Problem Description
A long time ago there are no himalayas between India and China, the both cultures are frequently exchanged and are kept in sync at that time, but eventually himalayas rise up. With that at first the communation started to reduce and eventually died.



Let's assume from my crude drawing that the only way to reaching from India to China or viceversa is through that grid, blue portion is the ocean and people haven't yet invented the ship. and the yellow portion is desert and has ghosts roaming around so people can't travel that way. and the black portions are the location which have mountains and white portions are plateau which are suitable for travelling. moutains are very big to get to the top, height of these mountains is infinite. So if there is mountain between two white portions you can't travel by climbing the mountain.
And at each step people can go to 4 adjacent positions.

Our archeologists have taken sample of each mountain and estimated at which point they rise up at that place. So given the times at which each mountains rised up you have to tell at which time the communication between India and China got completely cut off.
 

Input
There are multi test cases. the first line is a sinle integer $T$ which represents the number of test cases.

For each test case, the first line contains two space seperated integers $N, M$. next $N$ lines consists of strings composed of $0, 1$ characters. $1$ denoting that there's already a mountain at that place, $0$ denoting the plateau. on $N + 2$ line there will be an integer $Q$ denoting the number of mountains that rised up in the order of times. Next $Q$ lines contain $2$ space seperated integers $X, Y$ denoting that at ith year a mountain rised up at location $X, Y$.

$T \leq 10$

$1 \leq N \leq 500$

$1 \leq M \leq 500$

$1 \leq Q \leq N*M$

$0 \leq X < N$

$0 \leq Y < M$
 

Output
Single line at which year the communication got cut off.

print -1 if these two countries still connected in the end.

Hint:



From the picture above, we can see that China and India have no communication since 4th year.
 

Sample Input
  
1 4 6 011010 000010 100001 001000 7 0 3 1 5 1 3 0 0 1 2 2 4 2 1
 

Sample Output
  
4
 

Source


BFS + 二分


#include <cstdio>  
#include <iostream>  
#include <cstring>  
#include <cmath>  
#include <algorithm>  
#include <queue>  
using namespace std;
#define maxn 511  
#define move Move  

char mp[maxn][maxn];
int op[maxn*maxn][2];
int n, m, q;
const int move[4][2] = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
bool vis[maxn][maxn];

bool legal(int x, int y) {
	if (x < 0 || y < 0 || x >= n || y >= m)
		return 0;
	return 1;
}
struct node {
	int x, y;
	node(int x, int y) : x(x), y(y) {}
	node() {}
};
node gg[maxn*maxn]; int tou, wei;

bool ok(int pos) {//是否联通  
	memset(vis, 0, sizeof vis);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (mp[i][j] == '1') {//cout << ".." << endl;  
				vis[i][j] = 1;
			}
		}
	}
	for (int i = 1; i <= pos; i++) {
		vis[op[i][0]][op[i][1]] = 1;
	}
	for (int i = 0; i < m; i++) {
		if (!vis[0][i]) {
			tou = wei = 0;
			gg[wei++] = node(0, i);
			vis[0][i] = 1;
			while (tou < wei) {
				node now = gg[tou++]; //cout << now.x << " " << now.y << endl;  
				for (int i = 0; i < 4; i++) {
					node next;
					next.x = now.x + move[i][0];
					next.y = now.y + move[i][1];
					if (!legal(next.x, next.y) || vis[next.x][next.y])
						continue;
					if (next.x == n - 1)
						return 1;
					gg[wei++] = next;
					vis[next.x][next.y] = 1;
				}
			}
		}
	}
	return 0;
}

int main() {
	//freopen ("in.txt", "r", stdin);  
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n; i++)
			scanf("%s", mp[i]);
		scanf("%d", &q); //cout << q << endl;  
		for (int i = 1; i <= q; i++) {
			scanf("%d%d", &op[i][0], &op[i][1]);
		}
		if (!ok(0)) {
			printf("0\n");
			continue;
		} 
		int l = 1, r = q;
		while (r - l > 1) {
			int mid = (l + r) >> 1;
			if (ok(mid))
				l = mid;
			else r = mid;
		}
		if (!ok(r))
			printf("%d\n", r);
		else
			printf("-1\n");
	}
	return 0;
}


二维上的并查集


/****************
*PID:
*Auth:Jonariguez
*****************
*/
#define lson k*2,l,m  
#define rson k*2+1,m+1,r  
#define rep(i,s,e) for(i=(s);i<=(e);i++)  
#define For(j,s,e) For(j=(s);j<(e);j++)  
#define sc(x) scanf("%d",&x)  
#define In(x) scanf("%I64d",&x)  
#define pf(x) printf("%d",x)  
#define pfn(x) printf("%d\n",(x))  
#define Pf(x) printf("%I64d",(x))  
#define Pfn(x) printf("%I64d\n",(x))  
#define Pc printf(" ")  
#define PY puts("YES")  
#define PN puts("NO")  
#include <stdio.h>  
#include <string.h>  
#include <string>  
#include <math.h>  
#include <set>  
#include <map>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <iostream>  
#include <algorithm>  
using namespace std;
typedef long long LL;
typedef int Ll;
Ll quick_pow(Ll a, Ll b, Ll MOD){ a %= MOD; Ll res = 1; while (b){ if (b & 1)res = (res*a) % MOD; b /= 2; a = (a*a) % MOD; }return res; }

const int maxn = 500 + 10;
int mp[maxn][maxn], X[maxn*maxn], Y[maxn*maxn];
int n, m, par[maxn*maxn], par2[maxn*maxn];
char str[maxn];
int dx[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[8] = { 1, 0, -1, 0, 1, -1, 1, -1 };

struct node{
	int x, y;
}s[maxn];

void Init(){
	for (int i = 0; i <= n*m; i++){
		par[i] = i; par2[i] = i;
	}
}

int ID(int i, int j) {
	return i*m + j;
}

int Find(int x) {
	return x == par[x] ? x : par[x] = Find(par[x]);
}

int Find2(int x){
	return x == par2[x] ? x : par2[x] = Find2(par2[x]);
}

void Union(int u, int v) {
	u = Find(u); v = Find(v);
	if (u%m<v%m) par[v] = u;//维护最左列  
	else par[u] = v;
}

void Union2(int u, int v) {
	u = Find2(u); v = Find2(v);
	if (u%m>v%m) par2[v] = u; //维护最右列  
	else par2[u] = v;
}

int main()
{
	int i, j, T;
	scanf("%d", &T);
	while (T--) {
		scanf("%d%d", &n, &m);
		Init();
		for (i = 0; i<n; i++) {
			scanf("%s", str);
			for (j = 0; j<m; j++)
				mp[i][j] = (str[j] - '0');
		}
		for (i = 0; i<n; i++) {
			for (j = 0; j<m; j++) {
				if (mp[i][j] == 1) {
					for (int k = 0; k<8; k++) {
						int x = i + dx[k], y = j + dy[k];
						if (x<0 || x >= n || y<0 || y >= m || mp[x][y] == 0) continue;
						Union(ID(i, j), ID(x, y));
						Union2(ID(i, j), ID(x, y));
					}
				}
			}
		}
		for (i = 0; i<n; i++) {
			for (j = 0; j<m; j++)
				Find(ID(i, j)); Find2(ID(i, j));
		}  //???
		int q;
		scanf("%d", &q);
		for (i = 1; i <= q; i++) {
			scanf("%d%d", &X[i], &Y[i]);
		}
		for (i = 1; i <= q; i++) {
			mp[X[i]][Y[i]] = 1;
			for (int k = 0; k<8; k++) {
				int x = X[i] + dx[k], y = Y[i] + dy[k];
				if (x<0 || x >= n || y<0 || y >= m || mp[x][y] == 0) continue;
				Union(ID(X[i], Y[i]), ID(x, y));
				Union2(ID(X[i], Y[i]), ID(x, y));
			}
			int t = Find(ID(X[i], Y[i])), d = Find2(ID(X[i], Y[i]));
			if (t%m == 0 && d%m == m - 1)
				break;
		}
		if (i <= q)
			printf("%d\n", i);
		else puts("-1");
	}
	return 0;
}



PotPlayer自用皮肤 自用 PotPlayer 皮肤,仅微调了字体大小与颜色,是按个人习惯调整,未做适配性调整。 立即下载

相关推荐

2-shell及常用命令-续.pdf

2-shell及常用命令-续.pdf

HDU5652India and China Origins

India and China Origins   Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1995    Accepted Submission(s): 685     Problem Description A l...

Low Coder的博客 384

逆向实战:我是如何一步步解开美团外卖App的mtgsig3.0签名(附关键代码片段)

本文详细记录了逆向分析美团外卖App的mtgsig3.0签名算法的全过程,从抓包到算法还原,包括Java层和Native层的深度分析。通过Frida、IDA Pro等工具,揭示了签名生成的多层加密流程,并提供了关键代码片段。文章还探讨了逆向工程中的反调试对抗和签名时效性问题,为安全研究人员提供了宝贵的技术参考。

weixin_42530732的博客 872

hdu5652 India and China Origins(并查集)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:从indian到china隔着喜马拉雅山,喜马拉雅山不是一开始就产生的,大家地理都学过,喜马拉雅山是板块之间的挤压慢慢形成的,啊,扯得有点远。给你一开始喜马拉雅山的样子,然后每年都会生成新的山峰,问你第几年印度和中国不能往来。如果一直都可以往来,那么就输出-1; 题目分析:乍一看是

china震震的博客 411

开源libmodbus编译为32位和64位动态库

libmodbus 是支持Linux,Mac OS X,window 的一个开源库,支持modbus TCP和RTU 两种模式。

lemontree1945的博客 1923

C语言获取硬件信息(CPU序列号,硬盘序列号,网卡IP、MAC地址、是否插入网线)

本文主要介绍Linux和Windows下使用C语言获取各种硬件信息,包括CPU序列号、硬盘序列号、网卡信息(包括网卡名字、IP地址、MAC地址、网卡是否插入网线等)。

叶落花枯的博客 9378

2016蓝桥杯省赛C/C++B组7题剪邮票 DFS枚举组合情况BFS判联通

剪邮票 如【图1.jpg】, 有12张连在一起的12生肖的邮票。 现在你要从中剪下5张来,要求必须是连着的。 (仅仅连接一个角不算相连) 比如,【图2.jpg】,【图3.jpg】中,粉红色所示部分就是合格的剪取。 请你计算,一共有多少种不同的剪取方法。 请填写表示方案数目的整数。 注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。

EventQueue的博客 6970

MICS-6814多气体传感器与ESP32-C3 Mini接口构建空气监测系统

cararenuity C3 Mini的多个ADC引脚,Wi-Fi和蓝牙LE以及紧凑的设计使其成为嵌入式物联网应用的绝佳选择。在这个项目中,我们将把MICS-6814多气体传感器与基于esp32 -C3的开发板cararenuity C3 Mini连接起来。更多模拟引脚:与ESP8266只有一个ADC引脚不同,基于esp32 -C3的cararenuity C3 Mini提供多个ADC引脚,使其成为多传感器应用的理想选择。紧凑可靠:该板的小尺寸使其成为嵌入式应用的理想选择,而不会牺牲性能。

hie98894的博客 770

蓝桥杯练习系统试题集 算法提高 ADV-147 学霸的迷宫

问题描述   学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗。但学霸为了不要别人打扰,住在一个城堡里,城堡外面是一个二维的格子迷宫,要进城堡必须得先通过迷宫。因为班长还有妹子要陪,磨刀不误砍柴功,他为了节约时间,从线人那里搞到了迷宫的地图,准备提前计算最短的路线。可是他现在正向妹子解释这件事情,于是就委托你帮他找一条最短的路线。 输入格式   第一行两个整数n,

EventQueue的博客 3377

计算机毕业设计springboot基于微信平台的二次元手办商场小程序【附源码】

选题背景: 随着二次元文化的兴起和普及,越来越多的人开始对二次元角色产生了浓厚的兴趣和喜爱。手办作为二次元文化的重要载体之一,成为了许多二次元爱好者收藏和展示的对象。然而,在传统的线下手办商场中,消费者往往面临着时间、空间和选择的限制。因此,基于微信平台的二次元手办商场小程序应运而生。 选题意义: 首先,基于微信平台的二次元手办商场小程序能够打破地域限制,使得用户可以随时随地通过手机进行购物。无论是在家中、上班途中还是旅行中,用户都能够方便地浏览和购买自己喜欢的手办,极大地提高了购物的便利性和灵活性。

拉期程序 452

halcon3d点云补全方法

属于halcon实例中的内容,后面还接了一部分的匹配

Tianwen_running的博客 1231

团体程序设计天梯赛-练习集 L2-006 树的遍历 根据后序中序建树+BFS

L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。 输入格式: 输入第

EventQueue的博客 2219

黑猫带你学Nand第11篇:万字长文带你了解NandFlash的数据保持原理及特性(Data Retention)

TATTAT:Trap-assistedtunneling,陷阱辅助隧穿,也有叫法称为电荷陷阱现象。这是一种物理现象,它发生在非挥发性存储器,如NAND闪存。在这种效应中,存储在闪存单元浮动栅极上的电荷(电子)由于隧道氧化物中捕获的电荷形成的电场影响,导致电荷通过隧道效应从一个能级隧穿到另一个能级,从而造成电荷泄漏。这种电荷泄漏是导致闪存数据保持问题的主要原因之一,因为它可以改变存储在闪存单元中的电荷量,进而改变其阈值电压水平,最终可能导致数据错误。在NANDFlash中,电荷被存储在浮置栅极上。

黑猫奥利奥的博客 1096

蓝桥杯 历届试题 九宫重排 BFS Java

标准的BFS题,将九宫格哈希以后记录搜索的状态,用Set边搜索边去重,写的代码用了点面向对象的东西     AC代码: import java.util.*; public class Main { //f为初始状态,e为最终状态 static int f[][] = new int[3][3], e[][] = new int[3][3]; //将'.'表示成9这个数字 s

EventQueue的博客 1719

SpringBoot4+Java21前后端分离架构项目脚手架

本文介绍了基于SpringBoot 4和Java 21的前后端分离项目架构方案。主要内容包括:1)采用Spring Security 7实现JWT无状态认证和RBAC权限控制;2)后端使用MyBatis-Plus简化数据库操作,前端采用Vue3+Pinia组合;3)详细的项目结构设计,推荐按业务模块分包;4)核心依赖配置和架构设计图,涵盖认证授权流程。该方案结合最新技术栈,提供了生产级的全链路实现,特别适合需要快速构建现代化Web应用的项目。

Hello, New World! 421

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

pat甲级 1020. Tree Traversals map递归建树 + BFS

先用map建立一棵二叉树,mr存右孩子节点,ml存左孩子节点,然后输出BFS路径就行了 #include #include #include #include #include using namespace std; int N; int po[35], in[35], prin[35]; int root, ip, len; map ml, mr; int solve

EventQueue的博客 903

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

团体程序设计天梯赛 L3-008. 喊山 BFS

L3-008. 喊山 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 喊山,是人双手围在嘴边成喇叭状,对着远方高山发出“喂—喂喂—喂喂喂……”的呼唤。呼唤声通过空气的传递,回荡于深谷之间,传送到人们耳中

EventQueue的博客 776

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

pat 1127. ZigZagging on a Tree (30) 递归建树 + BFS

1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppose that all the keys in a binary

EventQueue的博客 745

hdu1181 变形课 BFS 判断连通性

变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 20132    Accepted Submission(s): 7240 Problem Description 呃......变形课上Harry碰到了一

EventQueue的博客 739

hdu1195 Open the Lock 广搜BFS 四维数组标记

Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5732    Accepted Submission(s): 2540 Problem Description Now an emerg

EventQueue的博客 703

poj3414 Pots BFS

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13400   Accepted: 5641   Special Judge Description You are given two pots, having the volume of A an

EventQueue的博客 646

hdu2614 Beat BFS or DFS

Beat Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1286    Accepted Submission(s): 751 Problem Description Zty is a man that alwa

EventQueue的博客 645

hdu5025 Saving Tang Monk BFS

Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2360    Accepted Submission(s): 848 Problem Description 《Journey t

EventQueue的博客 567

poj3278 Catch That Cow

本来用DFS做的,总是超时,拿别人AC的程序一测数据结果还一样,但是就是超时,这道题用DFS做要有很多剪枝,不然会有很多的重复,比如第x步走过点m,回溯后再DFS第y步的时候又走过m点,会有大量重复情况,还是BFS比较好剪枝 /* Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6327

EventQueue的博客 561

codeforces723D Lakes in Berland 搜索

D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The map of Berland is a rectangle of th

EventQueue的博客 530
上一篇: hdu1698 Just a Hook 线段树延迟标记区间更新 求加权区间总和
下一篇: poj3468 A Simple Problem with Integers 线段树延迟标记区间更新区间求和
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值