社区讨论
20分求助
P3956[NOIP 2017 普及组] 棋盘参与者 3已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @lo17oqvt
- 此快照首次捕获于
- 2023/10/22 16:33 2 年前
- 此快照最后确认于
- 2023/11/02 16:23 2 年前
CPP
#include<bits/stdc++.h>
using namespace std;
int mp[105][105];
bool vis[105][105];
struct Node
{
int x,y;
int gold;
bool f;
int color;
};
int m,n;
struct cmp
{
bool operator()(Node a,Node b)
{
return a.gold>b.gold;
}
};
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
bool check(int x,int y)
{
return x>=1 && y>=1 && x<=m && y<=m && !vis[x][y];
}
int bfs()
{
priority_queue<Node ,vector<Node> ,cmp> q;
q.push({1,1,0,true,mp[1][1]});
vis[1][1]=true;
while(!q.empty())
{
Node fr=q.top();
q.pop();
if(fr.x==m && fr.y==m)
{
return fr.gold;
}
for(int i=0;i<4;i++)
{
int tx=fr.x+dir[i][0];
int ty=fr.y+dir[i][1];
if(check(tx,ty))
{
if(!fr.f && mp[tx][ty]==-1)
{
continue;
}
if(mp[tx][ty]==-1)
{
q.push({tx,ty,fr.gold+2,false,fr.color});
}
else if(mp[tx][ty]==fr.color)
{
q.push({tx,ty,fr.gold,true,fr.color});
}
else if(mp[tx][ty]!=fr.color)
{
q.push({tx,ty,fr.gold+1,true,fr.color});
}
vis[tx][ty]=true;
}
}
}
return -1;
}
int main()
{
for(int i=0;i<105;i++)
{
for(int j=0;j<105;j++)
{
mp[i][j]=-1;
}
}
cin>>m>>n;
while(n--)
{
int x,y;
cin>>x>>y;
cin>>mp[x][y];
}
cout<<bfs()<<endl;
return 0;
}
不知道哪里出了问题,麻烦懂的dalao帮忙改进一下
回复
共 2 条回复,欢迎继续交流。
正在加载回复...