Kai’blog

主博客 | 势利纷华,近之而不染者洁,不近者亦洁,君子不立危墙之下。

P1825 诡异的题

一道不难的题,可是挑了好久也没调好!因为最开始写的代码太复杂了,一大堆嵌套括号,其中有一个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了。
诡异的是,我下面这个代码AC了:

#include <iostream>
#include <cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
using namespace std;

int N,M,b[310][310];
char m[310][310];
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;
				}
			}	
		}
	}
	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(nx<1||nx>N||ny<1||ny>M||m[nx][ny]=='#'||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<<-1<<endl;
    return 0;
}

为什么说这个代码诡异呢,因为这代码本质上和上一个是一样的,上一个是把越界数据设置为墙,然后用不撞墙代码来确保不越界,后一个是直接确保不越界。凭什么后一个导致RE?一定要研究清楚。

另外,还有一个诡异的问题。当输入某组数据(一会我给出)时,理应输出75,洛谷的评测数据是75,我的程序也算出了75。当我自己用肉眼观察那组数据时,发现这个迷宫应该没有解呀应该走不出来呀,怎么会75步就走出来了呢。
数据是这样的:

########################################################################################################################################################################################################
=.B####.#.##....#..###.#.#.#######Y#.#.##.####K..I###....####.#####.####.#.#.########.#...#F#####.##..####......#.##.#######..##.#.#######.#..###.#F......P###..##..##..#.###..###.#.####M.#.###.##.####
####.######.D######..#.#####S.####P#...##.##.##########.##.##.####.##...#########.#.##...#..J.#..#.#.#..######.#N####.##.###.....L####.L..#..#EE#..#.#..###.####..#####..#...Q#####.#####K..#.##.####..#
#######.#.#..#.#...###..##I...#####.#####.####..##.#####.#####..#####.##..#..####.#######.....#..##.#####.#####..#...###.....#####.#..#...#.#...G#...###...##...####.#####....#.##########....#####...##
######.#.#..###..##.##.##.#########.####.#.#.#.###.#.#.###..R##.#.####.####.#.#..####..###########.###.##.#.Q###.#.##.##..####..#####.##B###..#..##..#####.###...##..########.####.#.#..###.##J.##..#..#
##X...#.##...###..########...#.#.##.###......#####.#####.#A####.##.#.##.#..###.##..#####....######..#...####..##.#H..#O...###.##..##.####.#.#.##N.#.####.########..####.##.##..###########...##X#..#..##
#####.###..###.###.#####....##.####.####.#.#..#.#..###.###..###.#.#####..###.###...###.#####.####.##.###...#.#..###.#############.##.##..###.#G##.##..######..###.###.#.####.####.###.###.#..##...##.###
###A..##..#####..#..#.###.##...#YC#.#O...##.#..........######.###..#.#.####.###..#######....##..#.###.#..#..#...##.##M..#.#..#...#.##.####.##.#S####.###.##..#.#######.######.#D######.#..#......###..##
######R..#.###.##.#.#.##.###..######.##..#.##..#####..Z###..#H.#.####.#.#.#.#ZC##...#...#.....###.#..###...#####.###..##.####..##.#.##..#####.##########.#.#..#.#...####.####...###...##...#..###.#...@#
########################################################################################################################################################################################################

这个迷宫我自己从出口倒着走,发现所有路都是死胡同,这就诡异了,我和洛谷的算法同时算出75步能走出,可我自己观察这个迷宫时却发现走不出来

posted @ 2023-09-27 15:18  Kai-G  阅读(8)  评论(0编辑  收藏  举报
Copyright © 2019-2020 拱垲. All rights reserved.