UVA 232 纵横字谜的答案

本文介绍了一个使用C++实现的填字游戏解析器,该解析器通过判断起始格子并遍历单词,最终生成Across和Down两部分的答案。代码中包含了字符串处理、条件判断和数组操作等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Code:

#include<cstring>
#include<cstdio>
#include<string>
using namespace std;

char s[15][15];
int sgd[15][15];//记录是否为起始格 
int r,c;

//判断起始格子 
bool is_start(int x, int y){
	if(s[x][y] == '*') return false;
	if(y == 0 || x == 0)
		return true;
	if(s[x-1][y] == '*' || s[x][y-1] == '*')
		return true;
	return false;
}
//是否在word内 
bool judge(int x, int y){
	if(x < 0 || x >= r || y < 0 || y >= c)
		return false;
	if(s[x][y] == '*')
		return false;
	return true;
}

int main()
	{
		int kcase = 0;
		while(scanf("%d",&r) == 1){
			memset(sgd,0,sizeof(sgd));
			if(!r) break;
			scanf("%d",&c);
			for(int i = 0; i < r; ++i){
				scanf("%s",s+i);
			}
			int cnt = 1;
			for(int i = 0; i < r; ++i){
				for(int j = 0; j < c; ++j){
					if(is_start(i,j)){
						sgd[i][j] = cnt++;
					}
				}
			}
			if(++kcase > 1) printf("\n");
			printf("puzzle #%d:\n",kcase);
			int sx = 0,sy = 0;
			bool ok = false;
			printf("Across\n");
			for(int i = 0; i < r; ++i){
				for(int j = 0; j < c; ++j){
					ok = false;
					if(sgd[i][j] > 0){
						sx = i; sy = j;
						ok = true;
						printf("%3d.",sgd[i][j]);
					}
					while(judge(sx,sy)){
						putchar(s[sx][sy]);
						++sy;
					}
					if(ok){
						i = sx; j = sy;
						printf("\n");
					}
				}
			}
			sx = sy = 0;
			printf("Down\n");
			for(int i = 0; i < r; ++i){
				for(int j = 0; j < c; ++j){
					ok = false;
					if(sgd[i][j] > 0){
						sx = i; sy = j;
						ok = true;
						printf("%3d.",sgd[i][j]);
					}
					while(judge(sx,sy)){
						putchar(s[sx][sy]);
						sgd[sx][sy] = 0;//关键 ,防止 重复的情况 
						++sx;
					}
					if(ok){
						printf("\n");
					}
				}
			}
		}
		
		return 0;
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值