• 【NOIP2014模拟赛No.1】我要的幸福


    OJ题号:ZHOJ1297

    思路:搜索。

    先预处理注定不能走的路径,然后dfs可以走的路径。

     1 #pragma GCC optimize ("O2")
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cctype>
     5 const int N=1001;
     6 int n,m,len,a[N][N]={{0}},path[N<<1];
     7 int getint() {
     8     register char ch;
     9     while(!isdigit(ch=getchar()));
    10     register int x=ch^'0';
    11     while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
    12     return x;
    13 }
    14 void printpath() {
    15     for(int i=0;i<len;i++) printf("%d ",path[i]);
    16     puts("");
    17 }
    18 void dfs(int x,int y) {
    19     if(!a[x][y]) return;
    20     path[x+y]=a[x][y];
    21     if((x+y+1)==len) {
    22         printpath();
    23         exit(0);
    24     }
    25     if((x+1)==n) {
    26         dfs(x,y+1);
    27         return;
    28     }
    29     if((y+1)==m) {
    30         dfs(x+1,y);
    31         return;
    32     }
    33     if(a[x+1][y]<a[x][y+1]) {
    34         dfs(x+1,y);
    35         dfs(x,y+1);
    36     }
    37     else {
    38         dfs(x,y+1);
    39         dfs(x+1,y);
    40     }
    41 }
    42 int main() {
    43     n=getint();
    44     m=getint();
    45     len=n+m-1;
    46     for(register int i=0;i<n;i++) {
    47         for(register int j=0;j<m;j++) {
    48             a[i][j]=getint();
    49         }
    50     }
    51     for(register int i=n-1;i>=0;i--) {
    52         for(register int j=m-1;j>=0;j--) {
    53             if((i==(n-1))&&(j==(m-1))) continue;
    54             if(!a[i+1][j]&&!a[i][j+1]) a[i][j]=0;
    55         }
    56     }
    57     if(a[n-1][m-1]) dfs(0,0);
    58     puts("Oh,the life is too difficult!");
    59     return 0;
    60 }
  • 相关阅读:
    zip加密-字符串加密成字符串
    语言学习第一天
    localStorage和sessionStorage数据存储
    ValueOf()和toString()
    call()与apply()区别typeof和instanceof的区别
    javaScript判断手机型号
    HTML&CSS
    Doctype 严格模式与混杂模式-如何触发这两种模式,区分它们有何意义?
    获得地址栏内的参数
    私有变量
  • 原文地址:https://www.cnblogs.com/skylee03/p/6955794.html
Copyright © 2020-2023  润新知