232 - Crossword Answers (UVA)

这道题因为我把puzzle打成了Puzzle,卡了我很久............真的太无语了。

题目链接如下:

Online Judge

我的代码如下:

cpp 复制代码
#include <cstdio>
#include <cctype>
#include <set>
const int maxx = 10;

int r, c, kase, cnt, temp;
char a[maxx][maxx];
int num[maxx][maxx];

int main(){
    kase = 0;
    while(scanf("%d", &r) == 1 && r){
        scanf("%d", &c);
        getchar();
        cnt = 0;
        for(int i = 0; i < r; ++i){
            for(int j = 0; j < c; ++j){
                scanf("%c", &a[i][j]);
                if(isalpha(a[i][j]) && (i == 0 || j == 0 || a[i - 1][j] == '*' || a[i][j - 1] == '*')){
                    num[i][j] = ++cnt;
                } else{
                    num[i][j] = -1;
                }
            }
            getchar();
        }
        printf("%s", 0 == kase ? "" : "\n");
        printf("puzzle #%d:\nAcross\n", ++kase);
        for(int i = 0; i < r; ++i){
            for(int j = 0; j < c; ++j){
                if(isalpha(a[i][j])){
                    printf("%3d.", num[i][j]);
                    while(j < c && isalpha(a[i][j])){
                        printf("%c", a[i][j]);
                        ++j;
                    }
                    printf("\n");
                }
            }
        }
        printf("Down\n");
        std::set<int> st;
        for(int j = 0; j < c; ++j){
            for(int i = 0; i < r; ++i){
                if(isalpha(a[i][j])){
                    st.insert(num[i][j]);
                    while(i < r && isalpha(a[i][j])){
                        ++i;
                    }
                }
            }
        }
        for(int i = 0; i < r; ++i){
            for(int j = 0; j < c; ++j){
                if(st.find(num[i][j]) != st.end()){
                    printf("%3d.", num[i][j]);
                    temp = i;
                    while(temp < r && isalpha(a[temp][j])){
                        printf("%c", a[temp][j]);
                        ++temp;
                    }
                    printf("\n");
                }
            }
        }
    }
    return 0;
}
相关推荐
LYFlied2 小时前
【每日算法】LeetCode 153. 寻找旋转排序数组中的最小值
数据结构·算法·leetcode·面试·职场和发展
唐装鼠3 小时前
rust自动调用Deref(deepseek)
开发语言·算法·rust
ytttr8734 小时前
MATLAB基于LDA的人脸识别算法实现(ORL数据库)
数据库·算法·matlab
jianfeng_zhu5 小时前
整数数组匹配
数据结构·c++·算法
smj2302_796826525 小时前
解决leetcode第3782题交替删除操作后最后剩下的整数
python·算法·leetcode
LYFlied6 小时前
【每日算法】LeetCode 136. 只出现一次的数字
前端·算法·leetcode·面试·职场和发展
唯唯qwe-7 小时前
Day23:动态规划 | 爬楼梯,不同路径,拆分
算法·leetcode·动态规划
做科研的周师兄7 小时前
中国土壤有机质数据集
人工智能·算法·机器学习·分类·数据挖掘
来深圳7 小时前
leetcode 739. 每日温度
java·算法·leetcode
yaoh.wang7 小时前
力扣(LeetCode) 104: 二叉树的最大深度 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽