当前位置: 代码迷 >> 综合 >> [UVa232]Crossword Answers用两个数组解决问题
  详细解决方案

[UVa232]Crossword Answers用两个数组解决问题

热度:65   发布时间:2024-01-19 07:04:00.0
先上代码
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define maxn 11
int main()
{int t = 0;int x, y;char a[maxn][maxn];int index[maxn][maxn];memset(index, 0, sizeof(index));while (cin>>x&&x){int temp = 0;memset(index, 0, sizeof(index));	cin >>  y;for (int x2 = 0; x2 < x;x2++)cin>>a[x2];for (int i = 0; i < x; i++){for (int j = 0; j < y; j++){if ((i == 0 || j == 0 || a[i - 1][j] == '*' || a[i][j - 1] == '*') && a[i][j] != '*'){temp++;index[i][j] = temp;}} }if (t++)cout << endl;cout << "puzzle #" << t << ":" << endl;cout << "Across\n";for (int i = 0; i < x; i++){int move = 0;while(move<y){if (index[i][move]){printf("%3d.", index[i][move]);while (move < y && a[i][move] != '*')printf("%c", a[i][move++]);cout << endl;}else{move++;}}}cout << "Down\n";for (int i = 0; i < x; i++){		int move_x = i;int move_y = 0;while (move_y < y){if (index[move_x][move_y]){printf("%3d.", index[move_x][move_y]);while (move_x < x&&a[move_x][move_y] != '*'){index[move_x][move_y] = 0;printf("%c", a[move_x++][move_y]);	}move_y++;move_x = i;cout << endl;}else move_y++;}		}}
}


本题的关键在于,用两个二维数组 ,一个是存储输入的字符,另一个用来标记起始位;

在输出ACROSS时没有多大问题加一个游标来标记列,从左到右,从上到下输出即可,

但在输出DOWN时就会发现如果只是单纯地把ACROSS的代码套过来,输出的起始位的顺序就变成从上到下,从左到右的顺序,因此我用了两个游标来在ACROSS代码的基础上进行变动。

但如果只是单纯地改变输出顺序,就会发现起始位会有重复的问题,于是我将输出的起始位都重置为0。

先上代码,上面是我在vjudge中通过 的代码,本地vs2013通过的代码如下

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define maxn 11
int main()
{int t = 0;int x, y;char a[maxn][maxn];int index[maxn][maxn];memset(index, 0, sizeof(index));while (cin>>x&&x){int temp = 0;memset(index, 0, sizeof(index));	cin >>  y;for (int x2 = 0; x2 < x;x2++)//输入puzzlecin>>a[x2];for (int i = 0; i < x; i++){for (int j = 0; j < y; j++){if ((i == 0 || j == 0 || a[i - 1][j] == '*' || a[i][j - 1] == '*') && a[i][j] != '*'){temp++;index[i][j] = temp;}} }if (t++)cout << endl;cout << "puzzle #" << t << ":" << endl;cout << "Across\n";for (int i = 0; i < x; i++){int move = 0;while(move<y){//cout << "here1" << endl;if (index[i][move]){//cout << "HERE2" << endl;printf_s("%3d.", index[i][move]);while (move < y && a[i][move] != '*')printf_s("%c", a[i][move++]);cout << endl;}else{//cout << "HERE3" << endl;move++;}//cout << "HERE4" << endl;//cout << move << endl;}//cout << "here5" << endl;}cout << "Down\n";for (int i = 0; i < x; i++)//从第一排到最后一排{		int move_x = i;int move_y = 0;while (move_y < y)//当列游标还没到最后一列时{if (index[move_x][move_y])//如果游标所到位置为起始位即值不为0时{printf_s("%3d.", index[move_x][move_y]);//输出起始位编号while (move_x < x&&a[move_x][move_y] != '*')//当排游标还没到最后一排且游标当前位置不是*时{index[move_x][move_y] = 0;//将当前起始位设为0,防止重复输出printf_s("%c", a[move_x++][move_y]);	//输出当前游标位的值且排游标自增1}move_y++;move_x = i;cout << endl;}else move_y++;//否则增加列游标}		}}
}


  相关解决方案