一道不难的题,可是挑了好久也没调好!因为最开始写的代码太复杂了,一大堆嵌套括号,其中有一个ny写成了nx一直没发现。后来用新定义变量取代了那些复杂的用函数、"."和方括号表达出来的量,尽管并没有让程序更快,却让我一眼便发现了错误,直接改正,教训是代码不要有太多中小括号嵌套,否则很难排错
原复杂代码:
#include <iostream>
#include <cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
using namespace std;
//1825
int N,M,b[305][305];
char m[305][305];
struct trs
{
int ax,ay,bx,by;
trs(){ax=ay=bx=by=0;}
};
struct pos
{
int x,y,t;
pos(){x=y=t=0;}
pos(int X,int Y,int T){x=X;y=Y;t=T;}
}npos;
map<char,trs> trsn;
string s;
const int kx[5]={0,0,0,1,-1},
ky[5]={0,1,-1,0,0};
int main()
{
cin>>N>>M;
for(int i=1;i<=N;i++)
{
cin>>s;
for(int j=0;j<M;j++)
{
m[i][j+1]=s[j];
if(s[j]=='@') npos.x=i,npos.y=j+1;
if('A'<=s[j]&&s[j]<='Z')
{
if(trsn[s[j]].ax==0)
{
trsn[s[j]].ax=i,trsn[s[j]].ay=j+1;
}
else
{
trsn[s[j]].bx=i,trsn[s[j]].by=j+1;
}
}
}
}
for(int i=1;i<=N;i++)m[i][0]=m[i][M+1]='#';
for(int i=1;i<=M;i++)m[0][i]=m[N+1][i]='#';
npos.t=0;//keyishan
b[npos.x][npos.y]=1;
queue<pos> q;
q.push(npos);
while(!q.empty())
{
npos=q.front();q.pop();
if(m[npos.x][npos.y]=='=')
{
cout<<npos.t<<endl;
return 0;
}
for(int i=1;i<=4;i++)
{
if(m[npos.x+kx[i]][npos.y+ky[i]]=='#')
continue;
if(b[npos.x+kx[i]][npos.y+ky[i]]==1) //判重
continue;
if(m[npos.x+kx[i]][npos.y+ky[i]]>='A'&&m[npos.x+kx[i]][npos.y+ky[i]]<='Z')
{ //cout<<m[npos.x+kx[i]][npos.y+ky[i]];
b[npos.x+kx[i]][npos.y+ky[i]]=1;
if(trsn[m[npos.x+kx[i]][npos.y+ky[i]]].ax==npos.x+kx[i]
&&trsn[m[npos.x+kx[i]][npos.y+ky[i]]].ay==npos.y+kx[i])
q.push(pos(trsn[m[npos.x+kx[i]][npos.y+ky[i]]].bx,trsn[m[npos.x+kx[i]][npos.y+ky[i]]].by,npos.t+1));
else
q.push(pos(trsn[m[npos.x+kx[i]][npos.y+ky[i]]].ax,trsn[m[npos.x+kx[i]][npos.y+ky[i]]].ay,npos.t+1));
}
else
{
b[npos.x+kx[i]][npos.y+ky[i]]=1;
q.push(pos(npos.x+kx[i],npos.y+ky[i],npos.t+1));
}
/*cout<<endl;cout<<endl;
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)cout<<b[i][j];
cout<<endl;
}*/
}
}
cout<<-1<<endl;
return 0;
}
神清气爽的新代码
#include <iostream>
#include <cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
using namespace std;
//1825
int N,M,b[305][305];
char m[305][305];
struct trs
{
int ax,ay,bx,by;
trs(){ax=ay=bx=by=0;}
};
struct pos
{
int x,y,t;
pos(){x=y=t=0;}
pos(int X,int Y,int T){x=X;y=Y;t=T;}
}npos;
map<char,trs> trsn;
string s;
const int kx[5]={0,0,0,1,-1},
ky[5]={0,1,-1,0,0};
int main()
{
cin>>N>>M;
for(int i=1;i<=N;i++)
{
cin>>s;
for(int j=0;j<M;j++)
{
m[i][j+1]=s[j];
if(s[j]=='@') npos.x=i,npos.y=j+1;
if('A'<=s[j]&&s[j]<='Z')
{
if(trsn[s[j]].ax==0)
{
trsn[s[j]].ax=i,trsn[s[j]].ay=j+1;
}
else
{
trsn[s[j]].bx=i,trsn[s[j]].by=j+1;
}
}
}
}
for(int i=1;i<=N;i++)m[i][0]=m[i][M+1]='#';
for(int i=1;i<=M;i++)m[0][i]=m[N+1][i]='#';
npos.t=0;//keyishan
b[npos.x][npos.y]=1;
queue<pos> q;
q.push(npos);
while(!q.empty())
{
npos=q.front();q.pop();
if(m[npos.x][npos.y]=='=')
{
cout<<npos.t<<endl;
return 0;
}
for(int i=1;i<=4;i++)
{
int nx=npos.x+kx[i],ny=npos.y+ky[i];
if(m[nx][ny]=='#')
continue;
if(b[nx][ny]==1) //判重
continue;
if(m[nx][ny]>='A'&&m[nx][ny]<='Z')
{
b[nx][ny]=1;
if(trsn[m[nx][ny]].ax==nx&&trsn[m[nx][ny]].ay==ny)
q.push(pos(trsn[m[nx][ny]].bx,trsn[m[nx][ny]].by,npos.t+1));
else
q.push(pos(trsn[m[nx][ny]].ax,trsn[m[nx][ny]].ay,npos.t+1));
}
else
{
b[nx][ny]=1;
q.push(pos(nx,ny,npos.t+1));
}
/*cout<<endl;cout<<endl;
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)cout<<b[i][j];
cout<<endl;
}*/
}
}
cout<<-1<<endl;
return 0;
}
但这个代码只有95分,有一个点RE了。
标签:int,305,pos,npos,ay,P1825,include From: https://www.cnblogs.com/gongkai/p/17732800.html