zoukankan      html  css  js  c++  java
  • hdu 1813(IDA*)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1813

    思路:首先bfs预处理出‘0’到边界点最短距离,然后构造 h() 为所’0‘点逃离迷宫的最少步数的最大值。

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 #include<queue>
      6 using namespace std;
      7 #define inf 1<<30
      8 typedef pair<int,int>PP;
      9 
     10 struct Node{
     11     int x,y;
     12 }node[44];
     13 
     14 char str[11][11];
     15 int map[11][11],Index[1111],dist[11][11];
     16 int n,m,maxdeep;
     17 int dir[4][2]={{0,1},{-1,0},{1,0},{0,-1}};
     18 
     19 bool In_Edge(int x,int y)
     20 {
     21     if(x==1||x==n||y==1||y==n)return true;
     22     return false;
     23 }
     24 
     25 int bfs(int x,int y)
     26 {
     27     queue<PP>que;
     28     que.push(make_pair(x,y));
     29     int dd[11][11];
     30     memset(dd,-1,sizeof(dd));
     31     dd[x][y]=0;
     32     while(!que.empty()){
     33         x=que.front().first,y=que.front().second;
     34         que.pop();
     35         if(In_Edge(x,y))return dd[x][y];
     36         for(int i=0;i<4;i++){
     37             int xx=x+dir[i][0],yy=y+dir[i][1];
     38             if(map[xx][yy]&&dd[xx][yy]==-1){
     39                 dd[xx][yy]=dd[x][y]+1;
     40                 que.push(make_pair(xx,yy));
     41             }
     42         }
     43     }
     44     return -1;
     45 }
     46 
     47 int Get_H(Node node[])
     48 {
     49     int max_dist=0;
     50     for(int i=0;i<m;i++){
     51         max_dist=max(max_dist,dist[node[i].x][node[i].y]);
     52     }
     53     return max_dist;
     54 }
     55 
     56 
     57 bool IDA_star(int deep,Node node[])
     58 {
     59     if(Get_H(node)+deep>maxdeep)return false;
     60     if(deep==maxdeep)return true;
     61 
     62     Node tmp_node[44];
     63     for(int i=0;i<4;i++){
     64         Index[deep]=i;
     65         for(int j=0;j<m;j++){
     66             int xx=node[j].x+dir[i][0],yy=node[j].y+dir[i][1];
     67             if(In_Edge(node[j].x,node[j].y)||map[xx][yy]==0){
     68                 tmp_node[j]=node[j];
     69             }else 
     70                 tmp_node[j].x=xx,tmp_node[j].y=yy;
     71         }
     72         if(IDA_star(deep+1,tmp_node))return true;
     73     }
     74     return false;
     75 }
     76 
     77 
     78 int main()
     79 {
     80     int t=0;
     81     while(~scanf("%d",&n)){
     82         if(t++)puts("");
     83         for(int i=1;i<=n;i++){
     84             scanf("%s",str[i]+1);
     85             for(int j=1;j<=n;j++)map[i][j]=1-(str[i][j]-'0');
     86         }
     87         m=0;
     88         for(int i=1;i<=n;i++){
     89             for(int j=1;j<=n;j++){
     90                 if(map[i][j]){
     91                     if(In_Edge(i,j))dist[i][j]=0;
     92                     else {
     93                         dist[i][j]=bfs(i,j);
     94                         node[m].x=i,node[m++].y=j;
     95                     }
     96                 }else 
     97                     dist[i][j]=inf;
     98             }
     99         }
    100         if(m==0)continue;
    101         for(maxdeep=1; ;maxdeep++){
    102             if(IDA_star(0,node))break;
    103         }
    104         for(int i=0;i<maxdeep;i++){
    105             if(Index[i]==0)puts("east");
    106             else if(Index[i]==1)puts("north");
    107             else if(Index[i]==2)puts("south");
    108             else if(Index[i]==3)puts("west");
    109         }
    110     }
    111     return 0;
    112 }
    113 
    114 
    115 
    116             
    View Code
  • 相关阅读:
    mybatis plus 执行sql
    springboot 打印 mybatisplus 的sql
    easyexcel 简单使用
    springboot ehcache
    easyexcel 时间转换
    springboot stopwatch
    c# 开机自动启动
    sqlHelper
    DirectX录音
    c# 发邮件
  • 原文地址:https://www.cnblogs.com/wally/p/3327938.html
Copyright © 2011-2022 走看看