UVA 705 - Slash Maze

时间限制:3.000秒

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=646


  把简单问题想复杂系列……

  入门经典第一版和训练指南上面的题,分类是给分到了图里面。

  其实要说题意也很简单,就是给你一迷宫图,问你最长的回路有多长。乍一看好办得很哪,dfs搜就是了。但是这道题蛋疼的地方就在于它给你的迷宫图是斜的,是由斜线和反斜线表示的。所以要想做,要么斜着搜,要么把迷宫掰正了搜。


  我用了后一种办法,然后就各种找规律掰迷宫啊,而且还是读一行掰一行啊……最后做完了去看别人的题解觉得真是把简单的问题想得极其复杂了……

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<vector>
#define ABS(x) ((x) > 0 ? x : -1 * (x))
using namespace std;
int num = 0;
bool ok = true;
bool have_answer = true;
char pic[400][400];
int cal_j(int i, int j, int m, int n) {
    return (m * 2 - 1) - (i - j) * 2;
}
int cal_i(int i, int j, int m, int n) {
    return j * 2 + 1 + i * 2;
}
void dfs(int i, int j) {
    if(pic[i][j] == '+') ok = false;
    if(pic[i][j] != ',' && pic[i][j] != ' ') return;
    if(pic[i][j] == ' ') num += 1;
    pic[i][j] = '.';
    dfs(i - 1, j);
    dfs(i, j + 1);
    dfs(i + 1, j);
    dfs(i, j - 1);
}
int main() {
    int T = 0, NUM = 0;
    int m, n;
    while(cin >> n >> m) {
        if(!m || !n) return 0;
        int maxi = 0, maxj = 0, maxanswer = 0, NUM = 0;
        memset(pic, ' ', sizeof(pic));
        char tpic[m][n];
        have_answer = false;
        for(int i = 0; i < m; i++) {
            string in;
            cin >> in;
            for(int j = 0; j < n; j++) {
                int ti = cal_i(i, j, m, n) + 1;
                int tj = cal_j(i, j, m, n) + 1;
                if(ti > maxi) maxi = ti + 3;
                if(tj > maxj) maxj = tj + 3;
                tpic[i][j] = in[j];
                if(in[j] == 92) {
                    pic[ti+1][tj-2] = pic[ti+1][tj-2] == ' ' ? ',' : pic[ti+1][tj-2];
                    pic[ti+1][tj-1] = pic[ti+1][tj-1] == ' ' ? ',' : pic[ti+1][tj-1];
                    pic[ti+1][tj] = pic[ti+1][tj] == ' ' ? ',' : pic[ti+1][tj];
                    pic[ti+1][tj+1] = pic[ti+1][tj+1] == ' ' ? ',' : pic[ti+1][tj+1];
                    pic[ti+1][tj+2] = pic[ti+1][tj+2] == ' ' ? ',' : pic[ti+1][tj+2];
                    pic[ti-1][tj] = pic[ti][tj] = pic[ti+1][tj] = pic[ti+2][tj] = pic[ti+3][tj] = '#';
                } else if(in[j] == 47) {
                    pic[ti-1][tj] = pic[ti-1][tj] == ' ' ? ',' : pic[ti-1][tj];
                    pic[ti][tj] = pic[ti][tj] == ' ' ? ',' : pic[ti][tj];
                    pic[ti+1][tj] = pic[ti+1][tj] == ' ' ? ',' : pic[ti+1][tj];
                    pic[ti+2][tj] = pic[ti+2][tj] == ' ' ? ',' : pic[ti+2][tj];
                    pic[ti+3][tj] = pic[ti+3][tj] == ' ' ? ',' : pic[ti+3][tj];
                    pic[ti+1][tj-2] = pic[ti+1][tj-1] = pic[ti+1][tj] = pic[ti+1][tj+1] = pic[ti+1][tj+2] = '#';
                }
            }
        }
        int have_painted[m][n];
        memset(have_painted, 0, sizeof(have_painted));
        for(int i = 0; i <= maxi; i++) pic[i][0] = pic[i][maxj] = '+';
        for(int j = 0; j <= maxj; j++) pic[0][j] = pic[maxi][j] = '+';
        for(int i = 0; i <= maxi; i++) {
            for(int j = 0; j <= maxj; j++) if(pic[i][j] == ' ') {
                ok = true;
                num = 0;
                dfs(i, j);
                if(ok && num > maxanswer) maxanswer = num, have_answer = true, NUM += 1;
                else if(ok) NUM += 1;
            }
        }
        cout << "Maze #" << ++T << ":" << endl;
        if(have_answer) cout << NUM << " Cycles; the longest has length " << maxanswer << "." << endl << endl;
        else cout << "There are no cycles." << endl << endl ;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值