uva 232 Crossword Answers

来源:互联网 发布:淘宝买家好评 编辑:程序博客网 时间:2024/06/11 21:26

https://vjudge.net/problem/UVA-232

题意:

输入一个r行c列的网格,黑格用“*”来表示,每个白格都填有一个字母。如果一个白格的左边或者上边的相邻位置没有白格(可能是黑格,也可能没有格子),则这个白格式一个起始格子。首先把所有的起始格子按照从上到下,从左到右的编号为1,2,3,……。找出所有的横向单词,这些单词必须从一个起始格子开始,这些单词必须从一个起始格子开始,向右延伸到一个黑格的左边或者整个网格的最右列。竖向单词类似。

思路:

简单模拟,不过需要注意起始格子的判断情况,其实也不难,用一个结构体保存结果,最后按照id排序就可以了。

代码:

  1 #include <stdio.h>  2 #include <string.h>  3 #include <vector>  4 #include <string>  5 #include <iostream>  6 #include <algorithm>  7 using namespace std;  8   9 struct node 10 { 11     int id; 12     string s; 13 }; 14  15 char a[15][15]; 16 int b[15][15]; 17  18 vector<node> row; 19 vector<node> col; 20  21 bool cmp(node aa,node bb) 22 { 23     return aa.id < bb.id; 24 } 25  26 int main() 27 { 28     int cas = 0; 29  30     int r,c; 31  32     while (scanf("%d",&r) != EOF) 33     { 34         if (r == 0) break; 35  36         row.clear();col.clear(); 37  38         scanf("%d",&c); 39  40         int bh = 1; 41  42         for (int i = 0;i < r;i++) 43             scanf(" %s",a[i]); 44  45         for (int i = 0;i < r;i++) 46             for (int j = 0;j < c;j++) 47         { 48             if (a[i][j] != '*') 49             { 50                 if (i - 1 < 0 || j - 1 < 0) 51                 { 52                     b[i][j] = bh++; 53                 } 54                 else if (i - 1 >= 0 && a[i-1][j] != '*' && j - 1 >= 0 && a[i][j-1] != '*') continue; 55                 else b[i][j] = bh++; 56  57             } 58         } 59  60         for (int i = 0;i < r;i++) 61         { 62             int st = 0; 63  64             while (st < c) 65             { 66                 string tmp; 67  68                 while (a[i][st] == '*') st++; 69  70                 int id = b[i][st]; 71  72                 for (;a[i][st] != '*' && st < c;st++) 73                 { 74                     tmp.push_back(a[i][st]); 75                 } 76  77  78                 if (tmp.size() > 0) 79                 { 80                     node tt; 81                     tt.s = tmp; 82                     tt.id = id; 83  84                     row.push_back(tt); 85                 } 86             } 87  88         } 89  90  91         for (int j = 0;j < c;j++) 92         { 93             int st = 0; 94  95             while (st < r) 96             { 97                 string tmp; 98  99                 while (a[st][j] == '*') st++;100 101                 int id = b[st][j];102 103                 for (;a[st][j] != '*' && st < r;st++)104                 {105                     tmp.push_back(a[st][j]);106                 }107 108 109                 if (tmp.size() > 0)110                 {111                     node tt;112                     tt.s = tmp;113                     tt.id = id;114 115                     col.push_back(tt);116                 }117             }118 119         }120 121 122         sort(row.begin(),row.end(),cmp);123         sort(col.begin(),col.end(),cmp);124 125         if (cas) puts("");126 127         printf("puzzle #%d:\n",++cas);128 129         printf("Across\n");130 131         for (int i = 0;i < row.size();i++)132         {133             printf("%3d.",row[i].id);134             cout << row[i].s << endl;135         }136 137         printf("Down\n");138 139         for (int i = 0;i < col.size();i++)140         {141             printf("%3d.",col[i].id);142             cout << col[i].s << endl;143         }144     }145 146 147     return 0;148 }

 

原创粉丝点击