• 【杭电】[1242]Rescue


    这里写图片描述
    这里写图片描述

    虽然学长把这题取名为BFS……
    不过一时脑抽没分清BFS是深搜还是广搜
    所以用的DFS做的

    不过对于这种水题其实都一样啦
    没什么坑点
    用一个二维数组保存每个点到终点的距离
    然后递归搜索
    遇见x就+2
    遇见.就+1

    #include<stdio.h>
    int inf=9999999;
    char map[220][220];
    int dis[220][220];
    int n,m;
    int mn,mm;
    int en,em;
    void bfs(int tn,int tm) {
        if(tm+1<m&&((map[tn][tm+1]=='.'&&dis[tn][tm+1]>=dis[tn][tm]+1)||(map[tn][tm+1]=='x'&&dis[tn][tm+1]>=dis[tn][tm]+2))) {
            dis[tn][tm+1]=dis[tn][tm]+1;
            if(map[tn][tm+1]=='x')
                dis[tn][tm+1]++;
            bfs(tn,tm+1);
        }
        if(tm-1>=0&&((map[tn][tm-1]=='.'&&dis[tn][tm-1]>=dis[tn][tm]+1)||(map[tn][tm-1]=='x'&&dis[tn][tm-1]>=dis[tn][tm]+2))) {
            dis[tn][tm-1]=dis[tn][tm]+1;
            if(map[tn][tm-1]=='x')
                dis[tn][tm-1]++;
            bfs(tn,tm-1);
        }
        if(tn-1>=0&&((map[tn-1][tm]=='.'&&dis[tn-1][tm]>=dis[tn][tm]+1)||(map[tn-1][tm]=='x'&&dis[tn-1][tm]>=dis[tn][tm]+2))) {
            dis[tn-1][tm]=dis[tn][tm]+1;
            if(map[tn-1][tm]=='x')
                dis[tn-1][tm]++;
            bfs(tn-1,tm);
        }
        if(tn+1<n&&((map[tn+1][tm]=='.'&&dis[tn+1][tm]>=dis[tn][tm]+1)||(map[tn+1][tm]=='x'&&dis[tn+1][tm]>=dis[tn][tm]+2))) {
            dis[tn+1][tm]=dis[tn][tm]+1;
            if(map[tn+1][tm]=='x')
                dis[tn+1][tm]++;
            bfs(tn+1,tm);
        }
    }
    int main() {
        while(scanf("%d %d",&n,&m)!=EOF) {
            getchar();
            for(int i=0; i<n; i++) {
                for(int j=0; j<m; j++) {
                    dis[i][j]=inf;
                    scanf("%c",&map[i][j]);
                    if(map[i][j]=='r') {
                        mn=i;
                        mm=j;
                        dis[i][j]=0;
                    } else if(map[i][j]=='a') {
                        en=i;
                        em=j;
                        map[i][j]='.';
                    }
                }
                getchar();
            }
            bfs(mn,mm);
            if(dis[en][em]==inf)
                printf("Poor ANGEL has to stay in the prison all his life.
    ");
            else
                printf("%d
    ",dis[en][em]);
        }
        return 0;
    }
    

    题目记录:【杭电】[1242]Rescue

  • 相关阅读:
    AJAX的使用
    django.template.exceptions.TemplateDoesNotExist: login.html报错
    cookie、session
    关于zipfile解压出现的字符编码问题
    使用jquery清空input 文本框中的内容
    DVWA SQL-injection 附python脚本
    关于itchat用法的一篇博文
    记录两个python itchat的用法博客网址
    pyinstaller 打包.exe文件记录遇到的问题
    用python编写的excel拆分小工具
  • 原文地址:https://www.cnblogs.com/BoilTask/p/12569625.html
Copyright © 2020-2023  润新知