当前位置: 首页 > news >正文

L3-1 夺宝大赛

输入样例 1:

5 7
1 1 1 1 1 0 1
1 1 1 1 1 0 0
1 1 0 2 1 1 1
1 1 0 0 1 1 1
1 1 1 1 1 1 1
7
1 5
7 1
1 1
5 5
3 1
3 5
1 4

输出样例 1:

7 6

样例 1 说明:

七支队伍到达大本营的时间顺次为:7、不可能、5、3、3、5、6,其中队伍 4 和 5 火拼了,队伍 3 和 6 火拼了,队伍 7 比队伍 1 早到,所以获胜。

输入样例 2:

5 7
1 1 1 1 1 0 1
1 1 1 1 1 0 0
1 1 0 2 1 1 1
1 1 0 0 1 1 1
1 1 1 1 1 1 1
7
7 5
1 3
7 1
1 1
5 5
3 1
3 5

输出样例 2:

No winner.

这道题直接bfs求最短路,但是我交了好几发一直卡两个点 wa,最后应该是不能用dis[i] != inf来判断是否是通路,而且这个题目x和y是反过来给的,反正挺别扭的,但是题目本身不是很难。 

 

#include "bits/stdc++.h"
using namespace std;
const int N = 103;
const int inf = 0x3f3f;
int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1}; 
int n, m;
int g[103][103];
int dis[N][N];
int done[N][N];
struct node{
	int x, y, dis;
	node(int a, int b, int c){
		x = a, y = b, dis = c;
	}
	bool operator < (const node &t) const{
		return dis > t.dis;
	}
};
struct n_node{
	int id, dis;
	n_node(int a, int b){
		id = a, dis = b;
	}
	bool operator < (const n_node & t) const{
		return dis > t.dis;
	}
};
void bfs(int x, int y){
	priority_queue<node> q;
	dis[x][y] = 0;
	memset(dis, inf, sizeof(dis));
	q.push(node(x, y, 0));
	while(q.size()){
		node t = q.top();
		done[t.x][t.y] = 1;
		q.pop();
		int xx = t.x, yy = t.y, diss = t.dis;
		for(int i = 0; i < 4; i ++){
			int x1 = xx + dir[i][0], y1 = yy + dir[i][1];
			if(x1 < 1 || x1 > n || y1 < 1 || y1 > m || g[x1][y1] == 0) continue;
			if(done[x1][y1] ) continue;
			if(dis[x1][y1] > diss + 1) dis[x1][y1] = diss + 1,done[x1][y1] = 1, q.push(node(x1, y1, diss + 1));			
		}
	}
	
}
int main(){	
	cin>>n>>m;
	int x, y;
	for(int i = 1; i <= n; i ++)
		for(int j = 1; j <= m; j ++){
			cin>>g[i][j];
			if(g[i][j] == 2) x = i, y = j;
		}
	bfs(x, y);
	int k;
	cin>>k;
	priority_queue<n_node> qq;
	for(int i = 1; i <= k; i ++){
		cin>>y>>x;
		if(!done[x][y]) continue;
		qq.push(n_node(i, dis[x][y]));
	}
	n_node t(n_node(-1, 0));
	int flag = 0;
	while(qq.size()){
		t = qq.top();
		qq.pop();
		if(!qq.size()) break;
		if(t.dis == qq.top().dis){
			while(qq.size() && qq.top().dis == t.dis) qq.pop();
			if(qq.size() == 0) flag = 1;
		}
		else {
			break;
		} 
	}
	if(t.id != -1  && !flag)
		cout<<t.id<<" "<<t.dis;
	else cout<<"No winner.";	
	
	return 0;
}

相关文章:

  • 【RAG从入门到精通系列】【RAG From Scratch 系列教程5: Retrieval】
  • 动态规划-第2篇
  • Java volatile 关键字详解
  • 华为HCIE认证用处大吗?
  • Python 字节码深度历险:dis 模块揭秘与性能优化实战
  • 数智读书笔记系列016 从《理解和改变世界》探寻AI时代的知识与智能密码
  • 命令行重启Ubuntu软件
  • 云服务器安装宝塔面板部署
  • Mac安装Neo4j图数据库
  • 使用 UNIX 命令在设计中搜索标识符:vcsfind 的入门指南
  • A测题目名称: 双机串口通信系统
  • obsidian插件:Tasks Calendar Wrapper,更清晰的任务列表系统
  • unity webgl导出嵌套html以及导入django
  • 友思特新品 | OCT-3D断层扫描成像测量系统OQ StrataScope升级2.0型号!
  • 【开源项目-爬虫】Firecrawl
  • windows C++ 申请大量内存
  • Spring boot3-Http Interface: 声明式编程
  • 第十九:channel 的使用
  • Docker容器安装软件(完整版)
  • 阿里云短信发送(工厂模式实现)
  • 官网网站域名与建设/网站推广的途径和方法
  • 网上学学网站开发工程师/友情链接联盟
  • 网站建设的费用需求/游戏推广员
  • .net 免备案网站空间/广告推销
  • 绵阳集团网站建设/重庆网站外包
  • 房产网站加盟/怎么搭建属于自己的网站