A* 合集
板子那篇烂尾了,等 \(25\) 年 \(csp\) 时再继续写吧
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int n,b[5000004];
int opt[5000003];
int x[5000004];
int a[5000004];
int y[5000004];
int len;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>opt[i]>>x[i];
if(opt[i]==2) cin>>y[i];
}
for(int i=0;i<=500005;i++) b[i]=i;
for(int i=n;i>=1;i--){
if(opt[i]==1){
a[++len]=b[x[i]];
}
else{
b[x[i]]=b[y[i]];
}
}
for(int i=len;i>=1;i--){
cout<<a[i]<<" ";
}
}
15数码问题
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int t_x[16]={3,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3};
int t_y[16]={3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2};
int opt_x[5]={1,0,0,-1};
int opt_y[5]={0,-1,1,0};
int step[60];
char opt[6]={'D','L','R','U'};
int ans;
int b[10][10];
int a[4][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};
void dfs(int x,int y,int s,int stp,int r,int last){
if(stp+s>r) return ;
if(!s) {
ans=stp;
return ;
}
int sum=stp;
for(int i=0;i<4;i++){
if(last+i==3) continue;
else{
int x_=x+opt_x[i],y_=y+opt_y[i];
if(x_<0||x_>3||y_<0||y_>3) continue;
int now_s=s;
int to=b[x_][y_];
now_s+=abs(x-t_x[to]);
now_s+=abs(y-t_y[to]);
now_s-=abs(x_-t_x[to]);
now_s-=abs(y_-t_y[to]);
step[stp+1]=i;
swap(b[x_][y_],b[x][y]);
dfs(x_,y_,now_s,stp+1,r,i);
if(ans!=1e9) return ;
swap(b[x_][y_],b[x][y]);
}
}
return ;
}
int T;
int c[100];
int main(){
cin>>T;
while(T--){
int s_x,s_y;
int num=0;
int ok=0;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
scanf("%d",&b[i][j]);
c[num]=b[i][j];
if(b[i][j]==0) s_x=i,s_y=j,ok+=3-num/4;
else for(int p=0;p<num;p++) if(c[p]>c[num]) ok++;
num++;
// cout<<num<<endl;
}
}
int sum=0;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(b[i][j]==a[i][j]||b[i][j]==0) continue;
int k=b[i][j];
sum+=abs(t_x[k]-i);
sum+=abs(t_y[k]-j);
}
}
ans=1e9;
for(int i=sum;i;i++){
dfs(s_x,s_y,sum,0,i,4);
if(ans!=1e9) break;
}
cout<<ans<<endl;
}
}
点击查看代码
#include<bits/stdc++.h>
using namespace std;
struct node{
int w,to,nxt;
}z[1000003];
int H[1000003];
int h[1000003];
int h_[1000004];
int cnt;
void add(int x,int y,int w){
z[++cnt].to=y;
z[cnt].nxt=h[x];
z[cnt].w=w;
h[x]=cnt;
}
void ADD(int x,int y,int w){
z[++cnt].to=y;
z[cnt].nxt=h_[x];
z[cnt].w=w;
h_[x]=cnt;
}
int vis[1000004];
int n,m,k;
int times[1000004];
typedef pair<int,int> pii;
typedef pair<int,pii> PII;
void dj(int s){
for(int i=1;i<=n;i++) vis[i]=0,H[i]=1e9;
priority_queue<pii,vector<pii>,greater<pii> > p;
H[s]=0;
p.push({0,s});
while(!p.empty()){
int x=p.top().second;
p.pop();
if(vis[x]) continue;
else{
vis[x]=1;
for(int i=h_[x];i;i=z[i].nxt){
int y=z[i].to;
if(H[y]>H[x]+z[i].w){
H[y]=H[x]+z[i].w;
p.push({H[y],y});
}
}
}
}
}
void A(){
priority_queue<PII,vector<PII>,greater<PII> > p;
p.push({H[n],{0,n}});
while(!p.empty()){
PII X=p.top();
p.pop();
int x=X.second.second;
int dis=X.second.first;
times[x]++;
if(x==1){
cout<<dis<<"\n";
if(times[1]==k) return ;
continue;
}
for(int i=h[x];i;i=z[i].nxt){
int y=z[i].to;
if(times[y]<k){
p.push({{z[i].w+dis+H[y]},{z[i].w+dis,y}});
}
}
}
}
int main(){
cin>>n>>m>>k;
for(int i=1;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
add(u,v,w);
ADD(v,u,w);
}
dj(1);
// for(int i=1;i<=n;i++) cout<<H[i]<<endl;
A();
for(int i=times[1]+1;i<=k;i++) cout<<"-1\n";
}
A* 合集的更多相关文章
- SQL Server技术内幕笔记合集
SQL Server技术内幕笔记合集 发这一篇文章主要是方便大家找到我的笔记入口,方便大家o(∩_∩)o Microsoft SQL Server 6.5 技术内幕 笔记http://www.cnbl ...
- 【Android】开发中个人遇到和使用过的值得分享的资源合集
Android-Classical-OpenSource Android开发中 个人遇到和使用过的值得分享的资源合集 Trinea的OpenProject 强烈推荐的Android 开源项目分类汇总, ...
- [Erlang 0122] Erlang Resources 2014年1月~6月资讯合集
虽然忙,有些事还是要抽时间做; Erlang Resources 小站 2014年1月~6月资讯合集,方便检索. 小站地址: http://site.douban.com/204209/ ...
- [Erlang 0114] Erlang Resources 小站 2013年7月~12月资讯合集
Erlang Resources 小站 2013年7月~12月资讯合集,方便检索. 附 2013上半年盘点: Erlang Resources 小站 2013年1月~6月资讯合集 小站地 ...
- SQL用法操作合集
SQL用法操作合集 一.表的创建 1.创建表 格式: 1 CREATE TABLE 表名 2 (列名 数据类型(宽度)[DEFAULT 表达式][COLUMN CONSTRAINT], 3 ... ...
- Python学习路径及练手项目合集
Python学习路径及练手项目合集 https://zhuanlan.zhihu.com/p/23561159
- Lucene搜索方式大合集
package junit; import java.io.File; import java.io.IOException; import java.text.ParseException; imp ...
- [Erlang 0105] Erlang Resources 小站 2013年1月~6月资讯合集
很多事情要做,一件一件来; Erlang Resources 小站 2013年1月~6月资讯合集,方便检索. 小站地址: http://site.douban.com/204209/ ...
- android 图像处理系列合集
为了便于大家对滤镜算法的学习,以后发布的图像处理滤镜系列帖子会在这里汇总,本人第一次写合集,写得不好的地方大家请见谅,手头上虽然有一些滤镜的算法,但是大多不是android版的,教程里的代码大多是我借 ...
- Android 自定义View合集
自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...
随机推荐
- 面试题53 - II. 0~n-1中缺失的数字
地址:https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof/ <?php /** 一个长度为n-1的递增排序数组中的所有数字都是唯一的, ...
- MyBatis与其使用方法讲解
ORM 在讲解Mybatis之前,我们需了解一个概念ORM(Object-Relational Mapping)对象关系映射,其是数据库与Java对象进行映射的一个技术.通过使用ORM,我们可以不用编 ...
- windows c++共享内存
#include <iostream> #include <Windows.h> #include <string> int main() { const wcha ...
- **Selenium IDE、Selenium RC 和 WebDriver 之间有什么区别?**
- 【技术分析】EIP-7702 场景下 EOA 授权签名的安全探讨
EIP-7702 在 2025 年即将到来的以太坊 Pectra 升级中,将会引入 EIP-7702 这个提案.其主要的内容就是使得 EOA 账户拥有了自己的 Storage ,并且可以通过 dele ...
- 通过 API 将Deepseek响应流式内容输出到前端
要实现通过 API 将流式内容输出到前端,可以采用以下技术方案(以 Python 后端 + 前端 JavaScript 为例): 方案一:使用 Server-Sent Events (SSE) 这是浏 ...
- SecureCRT配置跳板机
跳板机(Jump Server),也称堡垒机,是一类可作为跳板批量操作远程设备的网络设备,是运系统管理员或运维人员常用的操作平台之一. 大家知道,在日常的开发中,有可能我们的本机不能够直接连线上的服务 ...
- 关于Transformer中Decoder模块是如何预测下一个字符的算法
关于Transformer模型的Encoder-Decoder模块网上介绍的文章非常多,写的非常详尽,可谓汗牛充栋,尤其关于注意力计算这块,不仅给出了公式而且还有具体的计算步骤.关于Transform ...
- 学习EXTJS6(10)面向对象的基础框架-2【统一的组件模型】很重要
用到ExtJS,UNIGUI是以ExtJS为基础的框架.因此掌握基础组件模型太重要了.确实就是让自己知道其所以然. Ext中所有可视组件都继承自Ext.Component. 1.Ext.Compone ...
- 0x03 搜索与图论
搜索与图论 广度优先搜索\(BFS\) 概念 广度优先搜索(Breadth-First Search)是一种图遍历算法,用于在图或树中按层次逐层访问节点.它从源节点(起始节点)开始,首先访问源节点的所 ...