zoukankan      html  css  js  c++  java
  • ZOJ 2594 Driving Straight(最短路)

    题意:求左下角到右上角的最短路,按照方向输出  

    解题思路:优先方向的广搜,格式错误判WA

    解题代码:

      1 // File Name: 2594.c
      2 // Author: darkdream
      3 // Created Time: 2013年09月07日 星期六 22时18分29秒
      4 
      5 #include<stdio.h>
      6 #include<string.h>
      7 #include<stdlib.h>
      8 #include<time.h>
      9 #include<math.h>
     10 #define LL long long
     11 int map[1000][1000];
     12 int visit[1000][1000];
     13 char str[1000][1000];
     14 //freopen("/home/plac/problem/input.txt","r",stdin);
     15 //freopen("/home/plac/problem/output.txt","w",stdout);
     16 int xadd[] = {0,0,1,-1,0,0,1,-1};
     17 int yadd[] = {1,-1,0,0,1,-1,0,0};
     18 int n, m;
     19 struct node{
     20     int  x , y ,k,last;
     21 }list[1000*1000];
     22 struct node1
     23 {
     24     int x, y ;
     25 }step[1000*1000];
     26 int is(int x,int y)
     27 {
     28     if(str[x][y] == '+')
     29         return 1; 
     30     return 0 ;
     31 }
     32 int main(){
     33     int t ;
     34     scanf("%d",&t);
     35     while(t--)
     36     {
     37         scanf("%d %d
    ",&n,&m);
     38         n = n*2-1;
     39         m = m*2-1;
     40         memset(map,0,sizeof(map));
     41         memset(visit,0,sizeof(visit));
     42         memset(step,0,sizeof(step));
     43         memset(list,0,sizeof(list));
     44         memset(str,0,sizeof(str));
     45         for(int i =1;i<= n;i ++)
     46         {
     47             gets(&str[i][1]);
     48             for(int j = 1;j <= m;j ++)
     49             {
     50                 if(str[i][j] == '+' || str[i][j] == '|' || str[i][j] == '-')
     51                     map[i][j] = 1; 
     52             }
     53         }
     54         list[1].x = n; 
     55         list[1].y = 1;
     56         list[1].last = 0 ; 
     57         list[1].k = 0 ; 
     58         int low = 1,high = 1 ;
     59         /*for(int i =1;i <= n;i ++)
     60           {
     61           for(int j = 1; j<=m;j++)
     62           printf("%d ",map[i][j]);
     63           printf("
    ");
     64           }*/
     65         while(low <= high)
     66         {
     67             if(list[low].x == 1 && list[low].y == m)
     68             {
     69                 break;
     70             }
     71                 for(int i = list[low].k ;i <= list[low].k+3; i++)
     72                 {
     73                     int tx = list[low].x + xadd[i];
     74                     int ty = list[low].y + yadd[i];
     75                     if(map[tx][ty] && !visit[tx][ty])
     76                     {
     77                         visit[tx][ty] = 1; 
     78                         high ++;
     79                         list[high].x = tx;
     80                         list[high].y = ty;
     81                         list[high].last = low;
     82                         list[high].k = i % 4;  
     83                     }
     84                 }
     85             low++;
     86         }
     87         int k = 0;
     88         while(list[low].last)
     89         {
     90             k++;
     91             step[k].x = list[low].x;
     92             step[k].y = list[low].y;
     93             low = list[low].last;
     94         }
     95         /*    for(int i = k ;i >= 1; i --)
     96             {
     97             printf("%d %d
    ",step[i].x,step[i].y);
     98             } */
     99         int ld,d;
    100         if(step[k].x == n-1)
    101         {
    102             printf("N
    ");
    103             ld = 1; 
    104         }else{
    105             printf("E
    ");
    106             ld = 2;
    107         }
    108         for(int i = k-1 ;i >= 1;i --){
    109             if(ld == 1)
    110             {
    111                 if(step[i].x - step[i+1].x == -1)
    112                 {
    113                     d = 1;
    114                     if(is(step[i+1].x,step[i+1].y))
    115                         printf("F");
    116                 }else if(step[i].y - step[i+1].y == 1){
    117                         printf("R");
    118                     d = 2; 
    119                 }else {
    120                         printf("L");
    121                     d = 4;
    122                 }
    123             }
    124             else if(ld == 2){
    125                 if(step[i].y - step[i+1].y == 1)
    126                 {
    127                     d = 2;
    128                     if(is(step[i+1].x,step[i+1].y))
    129                         printf("F");
    130                 }else if(step[i].x - step[i+1].x == 1){
    131                         printf("R");
    132                     d = 3; 
    133                 }else {
    134                         printf("L");
    135                     d = 1;
    136                 }
    137 
    138             }else if(ld == 3){
    139                 if(step[i].x - step[i+1].x == 1)
    140                 {
    141                     d = 3;
    142                     if(is(step[i+1].x,step[i+1].y))
    143                         printf("F");
    144                 }else if(step[i].y - step[i+1].y == 1){
    145                         printf("L");
    146                     d = 2; 
    147                 }else {
    148                         printf("R");
    149                     d = 4;
    150                 }
    151 
    152 
    153             }else {
    154                 if(step[i].y - step[i+1].y == -1)
    155                 {
    156                     d = 4;
    157                     if(is(step[i+1].x,step[i+1].y))
    158                         printf("F");
    159                 }else if(step[i].x - step[i+1].x == 1){
    160                         printf("L");
    161                     d = 3; 
    162                 }else {
    163                         printf("R");
    164                     d = 1;
    165                 }
    166 
    167 
    168             }
    169             ld = d;    
    170         }
    171         printf("
    ");
    172         if(t != 0 )
    173             printf("
    ");
    174     }
    175     return 0 ;
    176 }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    19.Mybatis之动态SQL
    18.Mybatis的配置文件标签属性自动提示
    17.Mybatis的基本使用及入门案例
    16.jQuery属性操作
    15.jQuery淡入淡出效果
    14.jQuery常用方法
    13.jQuery选择器
    12.JavaScript基础知识
    11.浅析Java中的final关键字
    10.基于Tomcat的SmartUplaod文件上传
  • 原文地址:https://www.cnblogs.com/zyue/p/3308151.html
Copyright © 2011-2022 走看看