UVA 227 Puzzle(基础字符串处理)

题目链接:

https://cn.vjudge.net/problem/UVA-227

  1 /*
  2 问题 输入一个5*5的方格,其中有一些字母填充,还有一个空白位置,输入一连串
  3 的指令,如果指令合法,能够得到一个移动后的方格就输出方格,不能就输出
  4 “This puzzle has no final configuration.” 
  5 
  6 解题思路
  7 模拟,一个一个的读入字符,包括空格,再读入若干行指令,如果指令表面合法,进入模拟看内容是否合法,不合法直接输出提示。
  8  
  9 易错点
 10 读入和控制结束问题,注意读入指令后有一个换行需要处理
 11 输出格式,将换行放在谜题数前面 
 12 */
 13 #include<cstdio>
 14 #include<cstring>
 15 
 16 int check(char *s);
 17 int move(char e[][11],int x, int y,char *s);
 18 int main()
 19 {
 20     char e[11][11],s1[100],s[1100],ch;
 21     int i,j,flag,t=0,x,y,lens;
 22     while(1){    
 23         for(i=1;i<=5;i++){
 24             gets(e[i]+1);
 25             if(i == 1 && e[1][1] == 'Z'){
 26                 return 0;//控制结束 
 27             }
 28         }
 29         
 30         //整合指令 
 31         memset(s,0,sizeof(char)*1100);
 32         memset(s1,0,sizeof(char)*100);
 33         while(1){
 34             scanf("%s",s1);
 35             strcat(s,s1);
 36             lens=strlen(s);
 37             if(s[lens-1] == '0')
 38                 break;
 39         }
 40         getchar();//注意吃掉一个换行 
 41         
 42         //找到空白位置 
 43         for(i=1;i<=5;i++){
 44             for(j=1;j<=5;j++){
 45                 if(e[i][j] == ' '){
 46                     x=i;
 47                     y=j;
 48                 }
 49             }
 50         }
 51         
 52         if(t != 0)//注意格式 
 53             printf("\n");
 54             
 55         printf("Puzzle #%d:\n",++t);
 56         if(check(s)){
 57             if(move(e,x,y,s))
 58                 for(i=1;i<=5;i++){
 59                     for(j=1;j<=5;j++){
 60                         if(j == 1)
 61                             printf("%c",e[i][j]);
 62                         else
 63                             printf(" %c",e[i][j]);
 64                     }
 65                     printf("\n");
 66                 }
 67             else
 68                 printf("This puzzle has no final configuration.\n");
 69         }
 70         else
 71             printf("This puzzle has no final configuration.\n");
 72     }
 73     return 0;
 74 } 
 75 
 76 int move(char e[][11],int x, int y,char *s){
 77     int len=strlen(s),i;
 78     for(i=0;i<len-1;i++){
 79         if(s[i] == 'A'){
 80             if(x-1 < 1) return 0;
 81             e[x][y]=e[x-1][y];
 82             e[x-1][y]=' ';
 83             x--;
 84         }
 85         else if(s[i] == 'B'){
 86             if(x+1 > 5) return 0;
 87             e[x][y]=e[x+1][y];
 88             e[x+1][y]=' ';
 89             x++;
 90         }
 91         else if(s[i] == 'R'){
 92             if(y+1 > 5) return 0;
 93             e[x][y]=e[x][y+1];
 94             e[x][y+1]=' ';
 95             y++;
 96         }
 97         else if(s[i] == 'L'){
 98             if(y-1 < 1) return 0;
 99             e[x][y]=e[x][y-1];
100             e[x][y-1]=' ';
101             y--;
102         }
103     }
104     return 1;    
105 }
106 
107 int check(char *s)
108 {
109     int len=strlen(s),i;
110     for(i=0;i<len-1;i++){
111         if(s[i] != 'A' && s[i] != 'B' && s[i] != 'R' && s[i] != 'L')
112             return 0;
113     }
114     return 1;
115 }

 

转载于:https://www.cnblogs.com/wenzhixin/p/8854968.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单词搜索迷宫(Word Search Puzzle)问题是一个经典的算法问题,其输入是一个二维的字符数组和一组单词,目标是找出字符数组网格中的所有单词。这些单词可以是水平的、垂直的或者是任意的对角线方向,所以需要查找8个不同的方向。解决这个问题的一种常见方法是使用回溯算法,具体步骤如下: 1. 遍历二维字符数组,对于每个字符,以其为起点开始搜索,搜索的方向包括水平、垂直和对角线方向。 2. 对于每个搜索到的单词,将其记录下来。 3. 重复步骤1和2,直到遍历完整个二维字符数组。 下面是一个使用C#语言实现的单词搜索迷宫算法的示例代码: ```csharp class WordSearchPuzzle { private char[,] grid; private HashSet<string> words; public WordSearchPuzzle(char[,] grid, HashSet<string> words) { this.grid = grid; this.words = words; } public void Solve() { int rows = grid.GetLength(0); int cols = grid.GetLength(1); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { Search(i, j, new StringBuilder()); } } } private void Search(int row, int col, StringBuilder sb) { if (row < 0 || row >= grid.GetLength(0) || col < 0 || col >= grid.GetLength(1)) { return; } sb.Append(grid[row, col]); string word = sb.ToString(); if (words.Contains(word)) { Console.WriteLine("Found '{0}' at [{1}, {2}] to [{3}, {4}]", word, row, col, row - sb.Length + 1, col - sb.Length + 1); } if (word.Length < 3) { Search(row + 1, col, sb); Search(row - 1, col, sb); Search(row, col + 1, sb); Search(row, col - 1, sb); Search(row + 1, col + 1, sb); Search(row - 1, col - 1, sb); Search(row + 1, col - 1, sb); Search(row - 1, col + 1, sb); } sb.Remove(sb.Length - 1, 1); } } // 使用示例 char[,] grid = new char[,] { {'t', 'h', 'i', 's'}, {'w', 'a', 't', 's'}, {'o', 'a', 'h', 'g'}, {'f', 'g', 'd', 't'} }; HashSet<string> words = new HashSet<string>() { "this", "two", "fat", "that" }; WordSearchPuzzle puzzle = new WordSearchPuzzle(grid, words); puzzle.Solve(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值