5 solutions

  • 2
    @ 2025-1-16 19:40:18

    C24zhaozicheng

    #include<bits/stdc++.h>
    using namespace std;
    char c[1001][1001];
    const int dx[8]={0,0,1,-1,-1,1,-1,1};
    //			上 下 左 右 左上 右下 左下 右上
    const int dy[8]={1,-1,0,0,-1,1,1,-1};
    int n,m;
    bool mp[1000010];
    int cnt=1,maxs;
    bool vis[1001][1001];
    void dfs(int x,int y)
    {
    	for(int i=0;i<4;i++)
    	{
    		int nx=x+dx[i];//走后的坐标
    		int ny=y+dy[i];
    		if(mp[c[nx][ny]]==true/*判断是否走过*/||nx<1||ny<1||nx>n||ny>m/*判断是否越界*/||vis[nx][ny]==true/*判断是否走过*/) continue;//剪枝
    		mp[c[nx][ny]]=true;//走了
    		vis[nx][ny]=true;//走了
    		cnt++;
    		dfs(nx,ny);//回溯
    		mp[c[nx][ny]]=false;//没走
    		vis[nx][ny]=false;//没走
    		cnt--;
    	}
    	maxs=max(maxs,cnt);//选择更大的
    }
    int main()
    {
    	cin >> n >> m;
    	for(int i = 1; i <= n; i++)
    	{
    		for(int j = 1;j <= m; j++)
    		{
    			cin >> c[i][j];
    		}
    	}
    	//一开始的位置为左上角
    	vis[1][1]=true;
    	mp[c[1][1]]=true;
    	dfs(1,1);
    	cout << maxs;
    	return 0;
    }
    
    
    • 0
      @ 2025-1-17 13:53:19
      #include <bits/stdc++.h>
      using namespace std;
      bool pchar[128];
      char ppp[21][21];
      int r,s,x=0,y=0,maxx=0;
      void p(int sum){
      	bool u=false;
      	if(x>0&&pchar[ppp[x-1][y]]==false){
      		u=true;
      		x--;
      		pchar[ppp[x][y]]=true;
      		p(sum+1);
      		pchar[ppp[x][y]]=false;
      		x++;
      	}
      	if(x+1<r&&pchar[ppp[x+1][y]]==false){
      		u=true;
      		x++;
      		pchar[ppp[x][y]]=true;
      		p(sum+1);
      		pchar[ppp[x][y]]=false;
      		x--;
      	}
      	if(y>0&&pchar[ppp[x][y-1]]==false){
      		u=true;
      		y--;
      		pchar[ppp[x][y]]=true;
      		p(sum+1);
      		pchar[ppp[x][y]]=false;
      		y++;
      	}
      	if(y+1<s&&pchar[ppp[x][y+1]]==false){
      		u=true;
      		y++;
      		pchar[ppp[x][y]]=true;
      		p(sum+1);
      		pchar[ppp[x][y]]=false;
      		y--;
      	}
      	if(!u)maxx=max(sum+1,maxx);
      }
      int main(){
      	cin>>r>>s;
      	for(int i=0;i<r;i++)for(int j=0;j<s;j++)cin>>ppp[i][j];
      	pchar[ppp[x][y]]=true;
      	p(0);
      	cout<<maxx;
      	return 0;
      }
      
      • 0
        @ 2025-1-16 19:00:15
        #include<bits/stdc++.h>
        //修复版
        using namespace std;
        char c[1001][1001];
        const int dx[8]={0,0,1,-1,-1,1,-1,1};
        const int dy[8]={1,-1,0,0,-1,1,1,-1};
        int n,m;
        int mp[1000010];
        int cnt=1,maxs;
        bool vis[1001][1001];
        void dfs(int x,int y){
        	for(int i=0;i<4;i++){
        		int nx=x+dx[i];
        		int ny=y+dy[i];
        		if(mp[c[nx][ny]]==1||nx<1||ny<1||nx>n||ny>m||vis[nx][ny]==1)continue;
        		mp[c[nx][ny]]=1;
        		vis[nx][ny]=1;
        		cnt++;
        		dfs(nx,ny);
        		cnt--;
        		mp[c[nx][ny]]=0;
        		vis[nx][ny]=0;
        	}
        	maxs=max(maxs,cnt);
        }
        int main(){
        	cin>>n>>m;
        	for(int i=1;i<=n;i++){
        		for(int j=1;j<=m;j++){
        			cin>>c[i][j];
        		}
        	}
        	vis[1][1]=1;
        	mp[c[1][1]]=1;
        	dfs(1,1);
        	cout<<maxs;
        	return 0;
        }
        
        • 0
          @ 2025-1-16 16:47:14
          #include<bits/stdc++.h>
          
          using namespace std;
          char c[1001][1001];
          //dx[],dy[]为走地图专用 
          const int dx[8]={0,0,1,-1,-1,1,-1,1}; 
          const int dy[8]={1,-1,0,0,-1,1,1,-1};
          int n,m;
          int cnt=1,maxs;
          int mp[1000010];
          bool vis[1001][1001];
          void dfs(int x,int y){
          	if(mp[c[x][y]]==1||vis[x][y]==1){//结束条件:走重复或重复字母 
          		return ;
          	}
          	for(int i=0;i<4;i++){
          		int nx=x+dx[i];//走上下左右 
          		int ny=y+dy[i];//
          		if(mp[c[nx][ny]]==1||nx<1||ny<1||nx>n||ny>m||vis[nx][ny]==1)continue;//不可走条件 
          		mp[c[x][y]]=1;//判断为走过 
          		vis[x][y]=1;//判断为重复字母 
          		cnt++;//次数加一 
          		dfs(nx,ny);
          		//回溯行动 
          		cnt--;//次数回溯减一 
          		mp[c[x][y]]=0;//判断为没走过
          		vis[x][y]=0;//判断为不重复字母 
          	}
          	maxs=max(maxs,cnt);//获取最大值 
          }
          int main(){ 
          	//深度优先搜索 
          	cin>>n>>m;
          	for(int i=1;i<=n;i++){
          		for(int j=1;j<=m;j++){
          			cin>>c[i][j];
          		}
          	}
          	dfs(1,1);//起始位置(左上角) 
          	cout<<maxs;
          	return 0;
          }
          
          • 0
            @ 2024-1-24 11:11:06
            #include <bits/stdc++.h>
            
            using namespace std;
            bool zm[26] = {0};
            bool jz[10000][21] = {false};
            char jz1[10000][21];
            int count1 = 0;
            
            int dfs(int x,int y,int r,int s){
            	int rt = 0; 
            	bool ok = true;
            	zm[jz1[x][y]-'A'] = true;
            	if(x-1>=0 and not zm[jz1[x-1][y]-'A']){
            		rt = dfs(x-1,y,r,s);
            		ok = false;
            	}
            	if(x+1<r and not zm[jz1[x+1][y]-'A']){
            		rt = dfs(x+1,y,r,s);
            		ok = false;
            	}
            	if(y-1>=0 and not zm[jz1[x][y-1]-'A']){
            		rt = dfs(x,y-1,r,s);
            		ok = false;
            	}
            	if(y+1<s and not zm[jz1[x][y+1]-'A']){
            		rt = dfs(x,y+1,r,s);
            		ok = false;
            	}
            	if(ok == true){
            		for(int i = 0;i < 26;i++){
            			zm[i] == true?rt+=1:rt+=0;
            		}
            		rt >= count1?count1 = rt:rt = count1;
            	}
            	zm[jz1[x][y]-'A'] = false;
            	return rt;
            }
            
            int main(){
            	int r,s;
            	cin >> r >> s;
            	for(int i = 0;i < r;i++){
            		for(int j = 0;j < s;j++){
            			jz[i][j] = 1;
            			cin >> jz1[i][j];
            		} 
            	}
            	cout << dfs(0,0,r,s);
            	return 0;
            }
            
          • 1

          Information

          ID
          698
          Time
          1000ms
          Memory
          256MiB
          Difficulty
          8
          Tags
          # Submissions
          258
          Accepted
          38
          Uploaded By