zoukankan      html  css  js  c++  java
  • 迷宫搜索变型——【USACO5.2.1】蜗牛的旅行

    无限RE后,换了个oj提交,AC了
    DFS搜索的关键是,找到下一步的方向,与准确的回朔
    View Code
    #include<stdio.h>

    int map[129][129];
    int Max,n;
    int f[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
    void dfs(int a,int b,int step)
    {
    if(step>Max)
    Max
    =step;

    int sa,sb,i,j;
    for(i=0;i<=3;i++)
    {
    sa
    =a+f[i][0];
    sb
    =b+f[i][1];
    int ta=sa,tb=sb,sstep=step;
    if(ta>n||ta<1||tb>n||tb<1)continue;
    if(map[ta][tb]!=0)continue;

    int add=0;
    sstep
    ++;

    while(ta<=n&&ta>=1&&tb<=n&&tb>=1&&map[ta][tb]==0)
    {
    map[ta][tb]
    =1;
    ta
    +=f[i][0];
    tb
    +=f[i][1];
    add
    ++;
    sstep
    ++;
    }

    if(ta<=n&&ta>=1&&tb<=n&&tb>=1&&map[ta][tb]==1)//遇到自己结束
    {
    sstep
    --;
    if(sstep>Max)
    Max
    =sstep;
    for(j=0;j<add;j++)
    {
    ta
    -=f[i][0];
    tb
    -=f[i][1];
    map[ta][tb]
    =0;
    }
    continue;
    }

    ta
    -=f[i][0];
    tb
    -=f[i][1];
    sstep
    --;
    add
    --;//退一步,这里走才是合法路
    dfs(ta,tb,sstep);
    for(j=0;j<=add;j++)
    {
    map[ta][tb]
    =0;
    ta
    -=f[i][0];
    tb
    -=f[i][1];
    }
    }
    }

    int main()
    {
    int m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
    int i,j,t;
    char s;
    for(i=1;i<=n;i++)
    {
    for(j=1;j<=n;j++)
    {
    map[i][j]
    =0;
    }
    }
    for(i=1;i<=m;i++)
    {
    getchar();
    scanf(
    "%c%d",&s,&t);
    int temp=s-'A'+1;
    map[t][temp]
    =2;
    }

    Max
    =0;
    map[
    1][1]=1;
    dfs(
    1,1,1);
    printf(
    "%d\n",Max);
    }
    return 0;
    }
  • 相关阅读:
    线程交互
    线程死锁
    多线程的同步-sychronized
    线程常见方法
    创建多线程
    消费!
    Redis基本认识
    在右键菜单中加入"在IDEA中打开" (Open in IDEA)
    安装coc.nvim时 报[coc.nvim] javascript file not found 错误的解决方案
    汇编语言的种类
  • 原文地址:https://www.cnblogs.com/huhuuu/p/1992642.html
Copyright © 2011-2022 走看看