zoukankan      html  css  js  c++  java
  • 3009Curling 2.0

    不知道哪里错了,一开始忘记条件说超过10就输出-1,结果老是超时

    #include "iostream"
    #include "algorithm"
    #define N 10000
    using namespace std;
    int rea[4][2]={{0,1},{-1,0},{1,0},{0,-1}};
    struct{
      int x,y;
    }s,e;
    
    int w,h,step,map[25][25],MIN;
    int min(int a,int b){return a>b?b:a;}
    
    void dfs(int x,int y){
      int i,j,k;
      for(i=0;i<4;i++){
        for(j=1;j<=20;j++){
          int newtemx=x+j*rea[i][0];
          int newtemy=y+j*rea[i][1];
          if(step>MIN||step>9)return;
          if(map[newtemx][newtemy]==3){MIN=min(MIN,step);/*for(k=1;k<=5;k++)cout<<map[1][k]<<' ';cout<<x<<" **** "<<y<<' '<<step<<endl;*/}
          if(map[newtemx][newtemy]==1&&j==1)goto l1;
          if(newtemx>h||newtemy>w||newtemx<0||newtemy<0)goto l1;
          if(map[newtemx][newtemy]==1){
            map[newtemx][newtemy]=0;step++;
            //for(k=1;k<=5;k++)cout<<map[1][k]<<' ';cout<<endl;cout<<newtemx-rea[i][0]<<" "<<newtemy-rea[i][1]<<endl;system("pause");
            dfs(newtemx-rea[i][0],newtemy-rea[i][1]);
            map[newtemx][newtemy]=1;step--;
            goto l1;
          }
        }
       l1:;
      }
      l2:;
    }
    
    int main(){
      int i,j;
      while(cin>>w>>h){
        if(w==0&&h==0)break;
        MIN=N;
        step=0;
        for(i=1;i<=h;i++){
          for(j=1;j<=w;j++){
            cin>>map[i][j];
            if(map[i][j]==2){s.x=i;s.y=j;}
            if(map[i][j]==3){e.x=i;e.y=j;}
          }
        }
        map[s.x][s.y]=0;
        dfs(s.x,s.y);
        if(MIN==N)cout<<-1<<endl;
        else cout<<MIN+1<<endl;
      }
    }
  • 相关阅读:
    word2010怎么把白色方框变成黑色方框?
    Ubuntu 14.04 安装 Sublime Text 3
    安装xmlspy之后,链接及邮箱等都用这个软件打开,怎样取消?
    SRAM、DRAM、SDRAM、DDR、DDR2、DDR3
    ROM和RAM区别
    shell脚本分析一
    重要网址
    vi/vim
    dump_stack使用
    BIOS、BootLoader、uboot对比
  • 原文地址:https://www.cnblogs.com/dowson/p/3344881.html
Copyright © 2011-2022 走看看