https://vjudge.net/contest/547627#problem/A
题解:三维立体空间,将时间看成步数,也就是要找出走出迷宫的最短路径,bfs题目;通过循环去寻找下一步的 x ,y, z。这道题最需要注意的就是对应关系,行对x,列对y,层对z,平时的x ,y,z 成习惯很容易导致数组对应的顺序不对。
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
const int N=50;
const int M=3e5+10;
using namespace std;
int sx,sy,sz,ex,ey,ez;
int l,r,c;//z,x,y
int dx[]= {1,0,-1,0,0,0};
int dy[]= {0,1,0,-1,0,0};
int dz[]= {0,0,0,0,1,-1};
char a[N][N][N];
int book[N][N][N]= {0};
int head,tail;
struct node
{
int x,y,z,t;
} que[M];
int bfs()
{
int flag=0;
head=tail=1;
que[tail].x=sx;
que[tail].y=sy;
que[tail].z=sz;
que[tail].t=0;
book[sz][sx][sy]=1;
tail++;
while(head<tail)
{
for(int i=0; i<6; i++)
{
int nx=que[head].x+dx[i];
int ny=que[head].y+dy[i];
int nz=que[head].z+dz[i];
if( nx>=1 && nx<=r && ny>=1 && ny<=c && nz>=1 && nz<=l && a[nz][nx][ny]!='#' && book[nz][nx][ny]==0 )
{
que[tail].x=nx;
que[tail].y=ny;
que[tail].z=nz;
que[tail].t=que[head].t+1;
book[nz][nx][ny]=1;
tail++;
}
if(nx==ex&&ny==ey&&nz==ez)
{
flag=1;
break;
}
}
if(flag==1)
break;
head++;
}
return que[tail-1].t;
}
int main()
{
while(scanf("%d%d%d",&l,&r,&c)!=EOF)
{
head=tail=1;
que[tail].t=0;
if(l==0&&r==0&&c==0)
break;
for(int i=1; i<=l; i++)
{
for(int j=1; j<=r; j++)
{
for(int k=1; k<=c; k++)
{
book[i][j][k]=0;
}
}
}
for(int i=1; i<=l; i++)
{
for(int j=1; j<=r; j++)
{
for(int k=1; k<=c; k++)
{
cin>>a[i][j][k];
if(a[i][j][k]=='S')
{
sz=i,sx=j,sy=k;
//cout<<sz<<" "<<sx<<" "<<sy<<endl;
}
if(a[i][j][k]=='E')
{
ez=i,ex=j,ey=k;
//cout<<ez<<" "<<ex<<" "<<ey<<endl;
}
}
}
}
int time=bfs();
if(time!=0)
printf("Escaped in %d minute(s).\n",time);
else
printf("Trapped!\n");
}
}
https://vjudge.net/contest/547627#problem/C
题意:需要找到一个二进制数m,让m是所给n的整数倍;
题解:由于二进制数只由1或者0组成,想要补零,可以选择m*10,想要补1,可以选择m*10+1;为了防止出现别的数字,可以从1开始不断调用自身去深搜,直到 m%n==0,然后输出m;在测试的时候发现 m 出现了负数,所以需要加判断条件;
#include <iostream>
#include <queue>
#include <math.h>
#include <algorithm>
using namespace std;
int n;
int flag;
void dfs(long long m)
{
if(flag==1||m<0)
return ;
if(m%n==0)
{
flag=1;
cout<<m<<endl;
return ;
}
dfs(m*10);
dfs(m*10+1);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
flag=0;
dfs(1);
}
return 0;
}
https://vjudge.net/contest/547627#problem/D
题解:
比较两个字符串是否相等,可以使用strcmp函数。
要实现判断要实现无论多少次都无法实现变为给定的字符串,可以在第一次操作的时候将操作后的字符串保留下来,然后每一次进入循环的时候,判断是否辅助字符串会变回原来的字符串。
字符数组的拆分和重新读入,通过设定变量和循环读入
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int N=2e3+10;
int length;
int main()
{
int t;
cin>>t;
int n=0;
while(t--)
{
char a[N]= {0},b[N]= {0},str[N]= {0},change[N]= {0},save[N]= {0};
n++;
cin>>length;
cin>>a>>b>>str;
long long int step=0;
long long int ans=-1;
int head1=0;
while(1)
{
int m=99999;
step++;
int head=0;
for(int i=0; i<length; i++)
{
change[head++]=b[i];
change[head++]=a[i];
if(step==1)//将第一次操作后的字符串保存下来
{
save[head1++]=b[i];
save[head1++]=a[i];
}
}
int flag=strcmp(change,str);//比较辅助字符和给出的字符是否相等
if(flag==0)
{
ans=step;
break;
}
if(step>2)
m=strcmp(change,save);//比较辅助字符和第一次操作后的字符串是否相等
if(m==0)
break;
else
{
for(int i=0; i<length; i++)
a[i]=change[i];
int t=0;
for(int j=length; j<length*2; j++)
b[t++]=change[j];
}
}
cout<<n<<" "<<ans<<endl;
}
}
https://vjudge.net/contest/547627#problem/F
题解:深搜题目,设定循环去实现八个方向的寻找,题目要求在白色周围找白色,所以要先找到有白色的一个点,然后再往周围去搜索,只要找到白色,答案就增加。
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
const int N = 105;
char a[N][N]={0};
int book[N][N]={0};
int dx[] = { 1,-1,0,0,1,1,-1,-1 };
int dy[] = { 0,0,1,-1,1,-1,1,-1 };
int m, n;
void dfs(int x,int y)
{
for(int i=0;i<8;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(nx>=0&&nx<m&&ny>=0&&ny<n&&a[nx][ny]=='@'&&book[nx][ny]==0)
{
book[nx][ny]=1;
a[nx][ny]='*';
dfs(nx,ny);
}
}
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
if(m==0) break;
int ans=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
book[i][j]=0;
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>a[i][j];
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<=n;j++)
{
if(a[i][j]=='@')
{
book[i][j]=1;
a[i][j]='*';
dfs(i,j);
ans++;
}
}
}
cout<<ans<<endl;
}
return 0;
}