zoukankan      html  css  js  c++  java
  • 迷宫程序

      1 #include <iostream>
      2 #include <ctime>
      3 #include <stdlib.h>
      4 #include <iomanip>
      5 using namespace std;
      6 
      7 int a[10][10] = { 0 };
      8 
      9 void show(int a[][10])
     10 {
     11     cout << "
    ------------------------------------------" << endl;
     12     for (int i = 0; i < 10; i++)
     13     {
     14         for (int j = 0; j < 10; j++)
     15         {
     16             cout << setw(3) << a[i][j];
     17         }
     18         cout << endl;
     19     }
     20 }
     21 
     22 void init(int a[][10])
     23 {
     24     int x, y;
     25     srand(time(0));
     26     for (int i = 0; i < 40; i++)
     27     {
     28         x = rand() % 10;
     29         y = rand() % 10;
     30         a[x][y] = 1;
     31     }
     32 }
     33 
     34 bool go(int a[][10], int x, int y, int dir,int flag)//判断某一个方向能不能走
     35 {
     36     switch (dir)
     37     {
     38     case 0://
     39         if (y < 9 && a[x][y+1] == 0)
     40         {
     41             a[x][y + 1] = flag;
     42             return true;
     43         }
     44         break;
     45     case 1://
     46         if (x < 9 && a[x+1][y] == 0)
     47         {
     48             a[x+1][y] = flag;
     49             return true;
     50         }
     51         break;
     52     case 2://
     53         if (y > 0 && a[x][y-1] == 0)
     54         {
     55             a[x][y - 1] = flag;
     56             return true;
     57         }
     58         break;
     59     case 3://
     60         if (x > 0 && a[x-1][y] == 0)
     61         {
     62             a[x-1][y] = flag;
     63             return true;
     64         }
     65         break;
     66     }
     67 
     68     return false;
     69 }
     70 
     71 int flag = 3;
     72 
     73 
     74 void search(int a[][10],int x,int y)
     75 {
     76     if (x == 9 && y == 9)
     77     {
     78         cout << "成功走出" << endl;
     79     }
     80     else
     81     {
     82         for (int i = 0; i < 4; i++)
     83         {
     84             if (go(a, x, y, i,  flag))//如果某一方向可以走 在go函数已经赋值过了
     85             {
     86                 flag ++;
     87                 show(a);
     88                 system("pause");
                 //根据方向进行递归
    89 switch (i) 90 { 91 case 0:// 92 search(a, x, y + 1);//如果向右 在这个位置进行一轮新的递归 93 a[x][y + 1] = 0;//某一个位置不满足要进行回溯 94 flag--; 95 break; 96 case 1:// 97 search(a, x + 1, y); 98 a[x+1][y] = 0; 99 flag--; 100 break; 101 case 2:// 102 search(a, x, y - 1); 103 a[x][y-1] = 0; 104 flag--; 105 break; 106 case 3:// 107 search(a, x - 1, y); 108 a[x - 1][y] = 0; 109 flag--; 110 break; 111 } 112 } 113 } 114 } 115 116 } 117 118 void main() 119 { 120 a[0][0] = 2; 121 init(a); 122 show(a); 123 search(a, 0, 0); 124 system("pause"); 125 }
  • 相关阅读:
    Excel sheet Column Title
    Add Two Numbers
    Add Binary
    Excel Sheet Column Number
    Lowest Common Ancestor of a Binary Search Tree
    Invert Binary Tree
    Move Zeroes
    Contains Duplicate
    Maximum Depth of Binary Tree
    Java实现二叉树的构建与遍历
  • 原文地址:https://www.cnblogs.com/xiaochi/p/5094581.html
Copyright © 2011-2022 走看看