1 条题解

  • 0
    @ 2025-4-7 21:19:29

    C++ :

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    using namespace std;
    
    char mt[9][9][9];
    
    void change(char mt[][9][9])
    {
    	for(int i=1;i<9;i++)
    	{
    		int j=1;
    		for(;j<=i;j++)
    			for(int k=1;k<9;k++)
    				mt[i][j][k]='.';
    		for(;j<9;j++)
    			for(int k=1;k<9;k++)
    				mt[i][j][k]=mt[i-1][j-1][k];
    	}
    }
    
    void printf(char mt[][9][9])
    {
    	for(int i=0;i<9;i++)
    	{
    		for(int j=1;j<=8;j++)
    		{
    			for(int k=1;k<=8;k++)
    				printf("%c",mt[i][j][k]);
    			printf("\n");
    		}
    		printf("****************************\n");
    		printf("****************************\n");
    	}
    }
    
    struct node
    {
    	int x;
    	int y;
    	int state;
    	int step;
    };
    
    int walk[9][2]=
    {
    	0,0,
    	1,0,
    	1,1,
    	0,1,
    	-1,0,
    	-1,-1,
    	0,-1,
    	-1,1,
    	1,-1,
    };
    
    int vis[9][9][9];
    
    int bfs()
    {
    	node p;
    	p.x=8;
    	p.y=1;
    	p.state=0;
    	queue<node>q;
    	q.push(p);
    	memset(vis,0,sizeof(vis));
    	change(mt);
    	vis[0][8][1]=1;
    	while(!q.empty())
    	{
    		node tmp=q.front();
    		if(tmp.x==1&&tmp.y==8)
    		{
    			return 1;
    		}
    		for(int i=0;i<9;i++)
    		{
    			node tmp=q.front();
    			tmp.x+=walk[i][0];
    			tmp.y+=walk[i][1];
    			if(tmp.x>8||tmp.x<1||tmp.y>8||tmp.y<1)
    				continue;
    			if(mt[tmp.state][tmp.x][tmp.y]=='S')
    				continue;
    			if(tmp.state<8)
    				tmp.state++;
    			if(mt[tmp.state][tmp.x][tmp.y]=='S')
    				continue;
    			if(vis[tmp.state][tmp.x][tmp.y])
    				continue;
    			vis[tmp.state][tmp.x][tmp.y]=1;
    			q.push(tmp);
    		}
    		q.pop();
    	}
    	return 0;
    }
    
    int main()
    {
    	int t;
    	//freopen("in.txt","r",stdin);
    	scanf("%d",&t);
    	getchar();
    	int tag=1;
    	while(t--)
    	{
    		printf("Case #%d: ",tag++);
    		for(int i=1;i<=8;i++)
    		{
    			for(int j=1;j<=8;j++)
    				scanf("%c",&mt[0][i][j]);
    			getchar();
    		}
    		mt[0][1][8]='.';
    		mt[0][8][1]='.';
    		if(bfs())
    			printf("Yes\n");
    		else
    			printf("No\n");
    		getchar();
    	}
    	return 0;
    }
    
    • 1

    信息

    ID
    1191
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者