3 条题解

  • 1
    @ 2022-10-2 14:15:17
    #include<bits/stdc++.h>
    using namespace std;
    int n,a[20],b[20],sum;
    bool ss(int x){
    if(x<2) return 0;
    for(int i=2;i*i<=x;i++){
    if(x%i==0) return 0;
    }
    return 1;
    }
    void dfs(int t){
    if(t>n){
    if(ss(a[n]+a[1])){
    for(int i=1;i<=n;i++)
    cout<<a[i]<<" ";
    cout<<endl;
    sum++;
    }
    return ;
    }
    for(int i=2;i<=n;++i){
    if(!b[i]&&ss(a[t-1]+i)){
    b[i]=1;
    a[t]=i;
    dfs(t+1);
    b[i]=0;
    }
    }
    }
    int main(){
    cin>>n;
    if(n%2==1){
    cout<<"Lie!";
    return 0;
    }
    a[1]=1;
    dfs(2);
    if(sum==0){
    cout<<"Lie!";
    return 0;
    }
    cout<<sum;
    }
    
    • 0
      @ 2022-10-2 14:16:54
      #include<bits/stdc++.h>
      using namespace std;
      char g[101][101];
      struct node{
      	int i,j;
      }f,sh;
      queue<node> q;
      bool b[101][101];
      int n,cx,cy,zx,zy,nx[5]={0,0,1,0,-1},ny[5]={0,1,0,-1,0},m;
      int bfs(){
      	while(!q.empty()){
      		f=q.front();
      		q.pop();
      		if(f.i==zx&&f.j==zy){
      			cout<<"YES"<<endl;
      			return 0;
      		}
      		for(int i=1;i<=4;i++){
      			int X=f.i+nx[i],Y=f.j+ny[i];
      			if(X<m&&X>=0&&Y<m&&Y>=0&&g[X][Y]=='.'&&b[X][Y]==0){
      				g[X][Y]='*';
      				b[X][Y]=1;
      				sh.i=X,sh.j=Y;
      				q.push(sh);
      			}
      		}
      	}
      	cout<<"NO"<<endl;
      	return 0;
      }
      int main(){
      	cin>>n;
      	for(int i=1;i<=n;i++){
      		cin>>m;
      		memset(b,0,sizeof(b));
      		for(int j=0;j<m;j++) for(int k=0;k<m;k++) cin>>g[j][k];
      		cin>>cx>>cy>>zx>>zy;
      		sh.i=cx,sh.j=cy;
      		while(!q.empty()) q.pop();
      		q.push(sh);
      		bfs();
      	}
      }
      
      • -5
        @ 2022-10-2 13:50:47
        #include<bits/stdc++.h>
        using namespace std;
        struct st{
        	int x,y;
        };
        int T,n,stax,stay,endx,endy;
        bool b[1000][1000];
        char a[1000][1000];
        int nx[4]={-1,0,1,0};
        int ny[4]={0,1,0,-1};
        queue<st>q;
        string bfs(){
        	while(!q.empty()){
        		st fr=q.front();
        		q.pop();
        		if(fr.x==endx&&fr.y==endy)return "YES";
        		st nf;
        		for(int i=0;i<=3;i++){
        			nf.x=fr.x+nx[i];
        			nf.y=fr.y+ny[i];
        			if(nf.x>=0&&nf.x<n&&nf.y>=0&&nf.y<n&&a[nf.x][nf.y]=='.'&&!b[nf.x][nf.y]){
        				b[nf.x][nf.y]=1;
        				q.push(nf);
        			}
        		}
        	}
        	return "NO";
        }
        int main(){
        	ios::sync_with_stdio(0);
        	cin>>T;
        	while(T--){
        		while(!q.empty())q.pop();
        		memset(b,0,sizeof(b));
        		cin>>n;
        		for(int i=0;i<n;i++)
        		for(int j=0;j<n;j++)cin>>a[i][j];
        		cin>>stax>>stay>>endx>>endy;
        		st sta;
        		sta.x=stax;
        		sta.y=stay;
        		b[stax][stay]=1;
        		q.push(sta);
        		cout<<bfs()<<endl;
        	}
            return 0;
        }
        
        • 1

        信息

        ID
        172
        时间
        1000ms
        内存
        256MiB
        难度
        4
        标签
        递交数
        111
        已通过
        36
        上传者