6 条题解

  • 5
    @ 2022-12-8 10:56:08
    #include <iostream>
    using namespace std;
    char a[150][150];
    int r[150][3];
    int n;
    void show(int k)
    {
        for(int i = 1;i < k;i++)
        {
            cout<<"("<<r[i][1]<<","<<r[i][2]<<")->";
        }
         
        cout<<"("<<r[k][1]<<","<<r[k][2]<<")"<<endl;
    }
    //从x、y点开始逐步探测 
    void num(int x,int y,int k)
    {
    	r[k][1] = x;
        r[k][2] = y;
        a[x][y] = '1';//探测过的点,设置为不可探测,防止重复的探测 
        if(x == n && y == n)
        {
            show(k);
        }
        else
        {
            //左、上、右、下探测,且不越界 
            if(y - 1 >= 1 && a[x][y - 1] == '0') num(x,y - 1,k+1);
            if(x - 1 >= 1 && a[x - 1][y] == '0') num(x-1,y,k+1);
            if(y + 1 <= n && a[x][y + 1] == '0') num(x,y + 1,k+1);
            if(x + 1 <= n && a[x + 1][y] == '0') num(x+1,y,k+1);
        }
    } 
    int main()
    {
        int i,j;
        cin>>n;
        //0表示能走,1表示不能走 
        for(i = 1;i <= n;i++)
        {
            for(j = 1;j <= n;j++)
            {
                cin>>a[i][j];
            }
        }
        num(1,1,1);
        return 0;
    }//已AC
    
    • 3
      @ 2023-6-6 21:13:45

      这道题用深搜dfs

      #include<bits/stdc++.h>
      using namespace std;
      bool f;
      char c[30][30];
      int n,a1[407],a2[407],dx[]={0,-1,0,1},dy[]={-1,0,1,0};
      bool flag;
      void re(int d1)
      {
          flag=1;
          for (int i=1;i<d1;i++)
              cout<<"("<<a1[i]<<","<<a2[i]<<")->";
          cout<<"("<<a1[d1]<<","<<a2[d1]<<")";
          return;
      }
      void dfs(int x,int y,int d)
      {
          if (flag)
              return;
          a1[d]=x,a2[d]=y;
          c[x][y]+=1;
          if (x==n&&y==n)
              re(d);
          for (int i=0;i<4;i++)
              if (x+dx[i]>0&&x+dx[i]<=n&&y+dy[i]>0&&y+dy[i]<=n&&c[x+dx[i]][y+dy[i]]=='0')
                  dfs(x+dx[i],y+dy[i],d+1);
          return;
      }
      int main()
      {
          cin>>n;
          for (int i=1;i<=n;i++)
          {
              string s;
              cin>>s;
              for (int j=1;j<=n;j++)
                  c[i][j]=s[j-1];
          }
          dfs(1,1,1);
          return 0;
      }
      • 0
        @ 2024-1-21 18:06:43

        这道题是一道典型深搜,让我们输出路线。难点在于存储走过的点。 解决方案:用二维数组b存储走过的x,y点,随后输出。

        #include <bits/stdc++.h>//b数组可以用结构体代替
        using namespace std;
        int n,b[400][10];
        char a[30][30];
        //方向的变化———左,上,右,下
        int fx[5]={0,0,-1,0,1};
        int fy[5]={0,-1,0,1,0};
        void print(int s){//打印第一条路线
            for (int i=1;i<=s;i++){
                cout<<'('<<b[i][1]<<','<<b[i][2]<<")";
                if (i!=s)cout<<"->";//不是最后一个便在后面加上->
            }
            exit(0);//停止程序
        }
        void fun(int x,int y,int k){//递归搜索
            //存储b数组,标记a数组
            a[x][y]='1';
            b[k][1]=x;
            b[k][2]=y;
            if (x==n&&y==n)print(k);//判断是否到了终点,到了终点便执行函数
            //循环方向
            for (int i=1;i<=4;i++){
                //求出当前点位在左,上,右,下后分别在哪
                int tx=x+fx[i];
                int ty=y+fy[i];
                if (a[tx][ty]=='0')fun(tx,ty,k+1);//判断(是否在迷宫内,是否被标记过)并递归求解
            }
        }
        int main(){
            //输入
            cin>>n;
            for (int i=1;i<=n;i++){
                for (int j=1;j<=n;j++)cin>>a[i][j];
            }
            //调用函数
            fun(1,1,1);
            return 0;
        }
        
        • 0
          @ 2023-1-10 19:31:26

          我用了dfs

          #include <bits/stdc++.h>
          using namespace std;
          char x;
          int n,m,a[105][105],used[105][105],minn = 1e9,num = 1;
          struct Point
          {
          	int xx,yy;
          } w[100005]; //w[i]表示第i步走到的位置,一定要定大一点,因为不知道要走多少步
          void f()
          {
          	cout << "(1,1)";
          	for (int i = 1;i <= num;i++)
          	{
          		cout << "->(" << w[i].xx << "," << w[i].yy << ")";
          	}
          }
          void dfs(int x,int y,int ans)
          {
          	if (a[x][y] == 1) return;
          	if (x > n || y > n) return;
          	if (x < 1 || y < 1) return;
          	w[ans] = (Point){x,y};
          	if (x == n && y == n)
          	{
          		num = ans;
          		f();
          		exit(0);
          	}
          	if (used[x][y] == 0)
          	{
          		used[x][y] = 1;
          		dfs(x,y-1,ans+1);     //顺序一定不能错!我调了好久才调对~(左上右下)
          		dfs(x-1,y,ans+1);
          		dfs(x,y+1,ans+1);
          		dfs(x+1,y,ans+1);
          		used[x][y] = 0;
          	}
          }
          int main()
          {
          	cin >> n;
          	for (int i = 1;i <= n;i++)
          	{
          		for (int j = 1;j <= n;j++)
          		{
          			cin >> x;
          			if (x != '1' && x != '0')  //第49~53行可以不要,这是防止输入错误。机器自动输入应该不会输错
          			{
          				j--;
          				continue;
          			}
          			a[i][j] = x-'0';
          		}
          	}
          	dfs(1,1,0);
          	return 0;
          }
          /*
          这是我的自制迷宫!!没事可以走走看!
          	21 24
          	0,0,1,0,0,0,1,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,1
          	1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,0,0,1,0,1,1,0,1
          	1,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,1,0,0,1,0,1
          	1,0,1,1,1,0,0,0,1,0,1,0,1,0,1,0,0,1,0,0,0,1,0,1
          	1,0,0,0,0,1,1,1,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,1
          	1,0,1,1,0,0,1,0,0,1,1,0,1,0,1,0,1,0,1,1,0,0,0,0
          	1,0,0,0,1,0,1,1,1,0,0,0,1,0,1,0,1,0,1,1,0,0,1,0
          	1,0,1,0,1,0,1,1,1,0,0,0,1,0,1,0,1,0,1,1,1,0,0,1
          	1,0,0,0,1,0,0,1,0,0,1,0,1,0,1,0,1,0,0,0,0,1,1,1
          	1,1,0,1,0,0,1,0,0,1,0,0,1,0,1,0,1,0,1,1,1,0,0,0
          	1,0,0,1,0,1,1,0,1,0,1,1,1,0,0,0,1,0,1,1,1,0,0,0
          	1,0,1,0,0,1,0,0,1,0,1,0,1,0,1,1,0,0,0,0,0,0,1,1
          	1,0,1,0,1,1,0,1,0,1,0,0,1,0,0,0,0,1,0,1,1,0,1,0
          	1,0,0,0,1,1,0,1,0,1,1,1,1,0,1,1,1,0,0,1,0,0,0,0
          	1,1,1,0,1,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,1,1,0
          	0,0,1,0,0,0,1,0,1,1,1,1,1,0,0,0,1,0,1,0,0,0,0,0
          	0,0,1,1,0,0,1,0,1,0,0,1,0,0,1,1,1,0,0,1,0,1,1,0
          	1,0,0,0,0,0,0,1,0,1,0,1,0,1,1,0,0,1,0,0,0,0,0,0
          	1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,1,1,1,1,1
          	1,0,0,1,0,1,0,1,0,1,0,1,0,1,1,0,0,1,0,1,1,0,0,0
          	1,1,0,0,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0
          */
          

          希望大家能点赞!

          • 0
            @ 2023-1-9 13:26:28

            大家能帮我看看哪里错了吗?33分(谢谢啦!)

            #include<bits/stdc++.h>
            using namespace std;
            int n;
            char a[25][25];
            struct Point
            {
            	int x,y;
            } d[25];  //存储迷宫路线
            int dx[4] = {-1,0,1,0},dy[4] = {0,1,0,-1};   //方向数组(左上右下)
            bool used[25][25];
            void dfs(int x,int y,int num)   //num表示走了几步
            {
            	used[x][y] = 1;
            	if (x == n && y == n)
            	{
            		printf("(1,1)");
            		for (int i = 1;i < num;i++)
            		{
            			printf("->(%d,%d)",d[i].x,d[i].y);
            		}
            		exit(0);
            	}
            	for (int i = 0;i < 4;i++)
            	{
            		int nx = x + dx[i],ny = y + dy[i];
            		if (nx < 1 || ny < 1 || nx > n || ny > n || a[nx][ny] == '1' || used[nx][ny] == 1)
            			continue;
            		d[num] = ((Point){nx,ny});
            		dfs(nx,ny,num+1);
            	}
            	used[x][y] = 0;
            }
            int main()
            {
            	ios::sync_with_stdio(false);
            	cin.tie(0);
            	cout.tie(0);
            	
            	cin >> n;
            	for (int i = 1;i <= n;i++)
            	{
            		for (int j = 1;j <= n;j++)
            		{
            			cin >> a[i][j];
            		}
            	}
            	dfs(1,1,1);
            	return 0;
            }
            
            • @ 2023-6-6 21:13:16

              我一开始敲的代码也是33,这个搜索顺序应该是下左上右,这是为什么呢()

          • 0
            @ 2022-4-24 17:48:49

            写题解请注意

            鼓励大家写题解,但注意题解格式。

            题解一定要有思路解析或代码注释,能否让别人理解你的思路

            也是你的能力的检验,不要只放无意义的代码给大家复制,那就失去了做题的初心。

            给代码两端加上这个会舒服一些

            ```cpp

            你的代码

            ```

            </span>

            这个点在键盘的左上角tab上面那个键,注意切换输入法

            #include<iostream>
            using namespace std;
            int main()
            {
                int n;
                cin>>n;//这是一个注释
                return 0;
            } 
            

            请注意严禁抄袭题解,写题解不要只放代码,需加上你的思路或代码注释。

            抄袭题解一经发现直接取消成绩。

            题解被删除的可能

            1. 代码不符合格式规范
            2. 没有思路讲解或者没有注释,
            3. 无意义的题解

            大家携手共同维护一个良好的编程环境,如果一经发现,多次作乱。可能会被管理员拉黑,请注意,一旦拉黑即失去登陆资格。

            • 1

            【基础】迷宫的第一条出路

            信息

            ID
            428
            时间
            1000ms
            内存
            128MiB
            难度
            4
            标签
            递交数
            149
            已通过
            67
            上传者