qq_27075621 2016-05-04 08:58 采纳率: 66.7%
浏览 1169

hdu1175连连看广搜WA,各大神走过路过看一看啊~

package cn.hncu.search;

import java.util.Scanner;

public class P1175Bfs {
static int[][] map;
static int[][] visited;
static int eh,el;//终点行列
static int h,l;//输入数据时限制的行列

private static void bfs(int ch, int cl) {//传入当前行列数
    int[][] dir={
            {0,1},  //向右走
            {1,0},  //向下走
            {0,-1}, //向左走
            {-1,0}  //向上走
    };
    int nh,nl;   //下一行,下一列
    Queue queue=new Queue();
    queue.push(ch, cl);
    while (!queue.isNull()) {
        Node node=queue.pop();
        if (node.x==eh&&node.y==el) {            //发现目标
            if (getTurnNum(queue)<=2) {
                System.out.println("YES");
                return;
            }else{
                System.out.println("NO");
                return;
            }
        }
        for (int i = 0; i < 4; i++) {
            nh=node.x+dir[i][0];
            nl=node.y+dir[i][1];
            if (nh<1||nh>h||nl<1||nl>l) {
                continue;
            }
            if ((map[nh][nl]==0&&visited[nh][nl]==0)||(nh==eh&&nl==el)) {
                visited[nh][nl]=1;
                queue.push(nh, nl);
            }
        }
    }
}



private static int getTurnNum(Queue queue) {
    Node[] nodes=queue.nodes;
    int tail=queue.tail;
    int turn=0;
    for (int i = tail-1; i >= 3; i--) {
        if (((nodes[i].x-1)==nodes[i-2].x&&(nodes[i].y-1)==nodes[i-2].y)      //爷爷节点的坐标是左上角,左下角,右上角,右下角
                ||((nodes[i].x-1)==nodes[i-2].x&&(nodes[i].y+1)==nodes[i-2].y)
                ||((nodes[i].x+1)==nodes[i-2].x&&(nodes[i].y-1)==nodes[i-2].y)
                ||((nodes[i].x+1)==nodes[i-2].x&&(nodes[i].y+1)==nodes[i-2].y)) {
            turn++;
        }
    }
    return turn;
}



public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    while (sc.hasNext()) {
        //初始化数据
        h=sc.nextInt();
        l=sc.nextInt();
        if ((h==0&&l==0)||(h==1&&l==1)) {
            return;
        }
        map=new int[h+1][l+1];
        visited=new int[h+1][l+1];

        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= l; j++) {
                map[i][j]=sc.nextInt();
            }
        }

        //输入测试数据
        int q=sc.nextInt();
        while (q-->0) {
            int sh=sc.nextInt();
            int sl=sc.nextInt();
            eh=sc.nextInt();
            el=sc.nextInt();
            if (map[sh][sl]!=map[eh][el]) {
                System.out.println("NO");
                continue;
            }
            if (map[sh][sl]==0||map[eh][el]==0) {
                System.out.println("NO");
                continue;
            }
            if ((sh==eh)&&(sl==el)) {
                System.out.println("NO");
                continue;
            }
            visited[sh][sl]=1;
            bfs(sh,sl);
        }
    }

}

}

class Queue{
Node[] nodes=new Node[1000000];
int head=1;
int tail=1;
int turn;

public Queue(){
    for (int i = 0; i < nodes.length; i++) {
        nodes[i]=new Node();
    }
}

public void push(int h,int l){
    if (tail<nodes.length) {
        nodes[tail].x=h;
        nodes[tail].y=l;
        tail++;
    }
}

public Node pop(){
    if (head<nodes.length) {
        Node node=new Node();
        node=nodes[head];
        head++;
        return node;
    }
    return null;
}

public boolean isNull(){
    if (head==tail) {
        return true;
    }
    return false;
}

}

//封装节点的行列
class Node{
int x;
int y;
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序