南阳oj-最少步数

没有权值考虑的广搜,不过大神用的神搜代码很简洁:

#include<cstdio>
#define min(x,y) x>y?y:x
int maze[9][9]={1,1,1,1,1,1,1,1,1,
    1,0,0,1,0,0,1,0,1,
    1,0,0,1,1,0,0,0,1,
    1,0,1,0,1,1,0,1,1,
    1,0,0,0,0,1,0,0,1,
    1,1,0,1,0,1,0,0,1,
    1,1,0,1,0,1,0,0,1,
    1,1,0,1,0,0,0,0,1,
    1,1,1,1,1,1,1,1,1};
int a,b,c,d,m;
void dfs(int x,int y,int s){
    if(maze[x][y]) return;
    if(x==c&&y==d){
        m=min(s,m);
        return;
    }
    s++;
    maze[x][y]=1;
    dfs(x+1,y,s);
    dfs(x,y+1,s);
    dfs(x-1,y,s);
    dfs(x,y-1,s);
    maze[x][y]=0;
}

int main(){
    int n;
    scanf("%d",&n);
    while(n--){
        m=9999;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        dfs(a,b,0);
        printf("%d\n",m);
    }
    return 0;
}

我的广搜:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

struct q
{
    int row;
    int col;
    int step;
};
int a,b,c,d;
int head = 1,tail =2;
q que[100];
int s1[9][9]={
 1,1,1,1,1,1,1,1,1,
 1,0,0,1,0,0,1,0,1,
 1,0,0,1,1,0,0,0,1,
 1,0,1,0,1,1,0,1,1,
 1,0,0,0,0,1,0,0,1,
 1,1,0,1,0,1,0,0,1,
 1,1,0,1,0,1,0,0,1,
 1,1,0,1,0,0,0,0,1,
 1,1,1,1,1,1,1,1,1};

int bfs(int a, int b, int c, int d)
{
    int s[9][9];
    memcpy(s,s1,sizeof(s1));
    head = 1,tail =2;
    que[head].row = a;
    que[head].col = b;
    que[head].step = 0;
    while(head < tail)
   {
       if(que[head].row == c && que[head].col == d )
       {
         return que[head].step;
       }
       int m = que[head].row;
       int n = que[head].col;
       if(s[m][n+1] == 0)
       {
           s[m][n+1] = 1;
           que[tail].row = m,que[tail].col  = n+1,que[tail].step = que[head].step +1;
           tail ++;
       }
        if(s[m+1][n] == 0)
       {

s[m+1][n] = 1;
           que[tail].row = m+1,que[tail].col = n,que[tail].step = que[head].step +1;
            tail ++;
       }
        if(s[m][n-1] == 0)
       {
           s[m][n-1] = 1;
           que[tail].row = m,que[tail].col = n-1,que[tail].step = que[head].step +1;
            tail ++;
       }
        if(s[m-1][n] == 0)
       {

s[m-1][n] =1;
           que[tail].row = m-1,que[tail].col = n,que[tail].step = que[head].step +1;
            tail ++;
       }
       head ++;
   }
}
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d%d%d%d",&a,&b,&c,&d);
      cout << bfs(a,b,c,d) << endl;
    }
    return 0;
}

时间: 2024-08-23 08:03:18

南阳oj-最少步数的相关文章

nyist oj 58 最少步数(dfs搜索)

最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 1,0,0,1,1,0,0,0,1 1,0,1,0,1,1,0,1,1 1,0,0,0,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,0,0,0,1 1,1,1,1,1,1,1,1,1 0表示道路,1表示墙. 现在输入一个道路的坐标作为

【南阳OJ分类之语言入门】80题题目+AC代码汇总

声明: 题目部分皆为南阳OJ题目. 代码部分包含AC代码(可能不止一个)和最优代码,大部分都是本人写的,并且大部分为c代码和少部分c++代码and极少java代码,但基本都是c语言知识点,没有太多差别,可能代码有的写的比较丑,毕竟知识有限. 语言入门部分题基本都较为简单,是学习编程入门的很好练习,也是ACM的第一步,入门的最佳方法,望认真对待. 本文由csdn-jtahstu原创,转载请注明出处,欢迎志同道合的朋友一起交流学习.本人QQ:1373758426和csdn博客地址. now begi

NYOJ 58 最少步数(BFS)

最少步数 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1  1,0,0,1,0,0,1,0,1  1,0,0,1,1,0,0,0,1  1,0,1,0,1,1,0,1,1  1,0,0,0,0,1,0,0,1  1,1,0,1,0,1,0,0,1  1,1,0,1,0,1,0,0,1  1,1,0,1,0,0,0,0,1  1,1,1,1,1,1,1,1,1 0表示道路,1表示墙. 现在输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才能从

最少步数(bfs)

最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 1,0,0,1,1,0,0,0,1 1,0,1,0,1,1,0,1,1 1,0,0,0,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,0,0,0,1 1,1,1,1,1,1,1,1,1 0表示道路,1表示墙. 现在输入一个道路的坐标作为

最少步数

最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 1,0,0,1,1,0,0,0,1 1,0,1,0,1,1,0,1,1 1,0,0,0,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,0,0,0,1 1,1,1,1,1,1,1,1,1 0表示道路,1表示墙. 现在输入一个道路的坐标作为

NYOJ 58 最少步数

最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 1,0,0,1,1,0,0,0,1 1,0,1,0,1,1,0,1,1 1,0,0,0,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,0,0,0,1 1,1,1,1,1,1,1,1,1 0表示道路,1表示墙. 现在输入一个道路的坐标作为

nyist 58 最少步数

最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1  1,0,0,1,0,0,1,0,1  1,0,0,1,1,0,0,0,1  1,0,1,0,1,1,0,1,1  1,0,0,0,0,1,0,0,1  1,1,0,1,0,1,0,0,1  1,1,0,1,0,1,0,0,1  1,1,0,1,0,0,0,0,1  1,1,1,1,1,1,1,1,1 0表示道路,1表示墙. 现在输入一

nyoj58最少步数

最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 1,0,0,1,1,0,0,0,1 1,0,1,0,1,1,0,1,1 1,0,0,0,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,0,0,0,1 1,1,1,1,1,1,1,1,1 0表示道路,1表示墙. 现在输入一个道路的坐标作为

最少步数(dfs)

最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 1,0,0,1,1,0,0,0,1 1,0,1,0,1,1,0,1,1 1,0,0,0,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,0,0,0,1 1,1,1,1,1,1,1,1,1 0表示道路,1表示墙. 现在输入一个道路的坐标作为

最少步数x

感觉好绕弯--噗 转过来就好啦~ §最少步数 §[问题描述] §    在各种棋中,棋子的走法总是一定的,如中国象棋中马走"日".有一位小学生就想如果马能有两种走法将增加其趣味性,因此,他规定马既能按"日"走,也能如象一样走"田"字.他的同桌平时喜欢下围棋,知道这件事后觉得很有趣,就想试一试,在一个(100*100)的围棋盘上任选两点A.B,A点放上黑子,B点放上白子,代表两匹马.棋子可以按"日"字走,也可以按"田&