社区讨论
30分求调
P3956[NOIP 2017 普及组] 棋盘参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @mk3s88iy
- 此快照首次捕获于
- 2026/01/07 16:54 上个月
- 此快照最后确认于
- 2026/01/10 16:15 上个月
CPP
#include<bits/stdc++.h>
using namespace std;
const int maxm=1e2+10;
int m,n,a[maxm][maxm],cost[maxm][maxm],dx[5]={0,0,0,-1,1},dy[5]={0,-1,1};
int bfs()
{
queue<pair<pair<int,int>,bool>>q;
q.push({{1,1},0});
cost[1][1]=0;
while(q.size())
{
int fx=q.front().first.first,fy=q.front().first.second,fstate=q.front().second;
for(int i=1;i<5;i++)
{
int nx=fx+dx[i],ny=fy+dy[i],nstate=fstate,ncost=cost[fx][fy];
if(nx>0&&nx<=m&&ny>0&&ny<=m)
{
if(a[nx][ny]==2)
{
if(nstate==0)
{
ncost+=2;
nstate=1;
a[nx][ny]=a[fx][fy];
}
else
{
continue;
}
}
else if(a[fx][fy]!=a[nx][ny])
{
ncost++;
}
if(cost[nx][ny]==-1||cost[nx][ny]>ncost)
{
cost[nx][ny]=ncost;
if(nx<m||ny<m)
{
if(fstate==1)
{
q.push({{nx,ny},0});
}
else
{
q.push({{nx,ny},nstate});
}
}
}
}
}
if(fstate==1)
{
a[fx][fy]=2;
}
q.pop();
}
return cost[m][m];
}
int main()
{
fill(&a[0][0],&a[0][0]+maxm*maxm,2);
memset(cost,-1,sizeof(cost));
int x,y,c;
cin>>m>>n;
for(int i=1;i<=n;i++)
{
cin>>x>>y>>c;
a[x][y]=c;
}
cout<<bfs();
return 0;
}
似乎是cost数组有问题
回复
共 2 条回复,欢迎继续交流。
正在加载回复...