• A. Maze


    题目链接:http://codeforces.com/problemset/problem/377/A

    思路:

    因为题目说了肯定存在一个解!所以第一次 dfs 之后我们就可以判断还差多少 ,然后找到后面可以继续 dfs的,直接在原先的基础上处理就可以了

    这题我本想找到一个符合要求的点去 dfs ,如果正好和 k 一致就输出 。不一致就再去找下一个符合的点   (这个方法的vis 数组需要去重置) 。正是因为这个缘故所以超时了

    代码也附上来吧,因为我觉得这个思想也是可以解决一些问题的!

    WA代码:

     1 #include <cstdio>
     2 #include <string>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <string.h>
     6 #include <math.h>
     7 #include <vector>
     8  
     9 using namespace std;
    10  
    11  
    12  
    13 int n,m,K,t;
    14 char maze[550][550];
    15 int vis[550][550];
    16 int nx[4] = {0,1,0,-1};
    17 int ny[4] = {1,0,-1,0};
    18 void dfs(int x, int y) {
    19     int tx,ty;
    20     for(int k = 0; k<4; k++) {
    21         tx = x+nx[k];
    22         ty = y+ny[k];
    23         if(tx <1 || tx > n || ty < 1 || ty > m) continue;
    24         if(maze[tx][ty] == '#' || vis[tx][ty]!=0) continue;
    25         vis[tx][ty] = 1;
    26         dfs(tx,ty);
    27         if (vis[tx][ty] != 666)
    28             vis[tx][ty] = 0;
    29     }
    30     if(K>0) {
    31         K--;
    32         vis[x][y]=666;
    33     }
    34 }
    35 int main()
    36 {
    37     cin>>n>>m>>K;
    38     t = K;
    39     for(int i = 1; i<=n; i++) {
    40         scanf("%s",maze[i]+1);
    41     }
    42     for(int i = 1; i<=n; i++) {
    43         for(int j = 1; j<=m; j++) {
    44             if(maze[i][j] == '.' && K > 0) {
    45                 K = t;
    46                 dfs(i,j);
    47             }
    48         }
    49     }
    50     for(int i = 1; i<=n; i++) {
    51         for(int j = 1; j<=m; j++) {
    52             if(vis[i][j]!=666)
    53                 printf("%c",maze[i][j]);
    54             else
    55                 printf("X");
    56         }
    57         printf("
    ");
    58     }
    59     return 0;
    60 }
    Ackerman

    AC代码:

     1 #include <cstdio>
     2 #include <string>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <string.h>
     6 #include <math.h>
     7 #include <vector>
     8  
     9 using namespace std;
    10  
    11  
    12  
    13 int n,m,K,t;
    14 char maze[550][550];
    15 int vis[550][550];
    16 int nx[4] = {0,1,0,-1};
    17 int ny[4] = {1,0,-1,0};
    18 void dfs(int x, int y) {
    19     int tx,ty;
    20     for(int k = 0; k<4; k++) {
    21         tx = x+nx[k];
    22         ty = y+ny[k];
    23         if(tx <1 || tx > n || ty < 1 || ty > m) continue;
    24         if(maze[tx][ty] == '#' || vis[tx][ty]!=0) continue;
    25         vis[tx][ty] = 1;
    26         dfs(tx,ty);
    27     }
    28     if(K>0) {
    29         K--;
    30         vis[x][y]=666;
    31     }
    32 }
    33 int main()
    34 {
    35     cin>>n>>m>>K;
    36     t = K;
    37     for(int i = 1; i<=n; i++) {
    38         scanf("%s",maze[i]+1);
    39     }
    40     for(int i = 1; i<=n; i++) {
    41         for(int j = 1; j<=m; j++) {
    42             if(maze[i][j] == '.' && K > 0) {
    43                 dfs(i,j);
    44             }
    45         }
    46     }
    47     for(int i = 1; i<=n; i++) {
    48         for(int j = 1; j<=m; j++) {
    49             if(vis[i][j]!=666)
    50                 printf("%c",maze[i][j]);
    51             else
    52                 printf("X");
    53         }
    54         printf("
    ");
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    Dsl学习笔记-3
    Dsl学习笔记-2
    Dsl学习笔记-1
    多线程学习笔记
    DataGrid研究笔记-3
    DataGrid研究笔记-2
    从基础到分析,聊一聊企业报表平台的建设规划!
    深度解读|数据化管理的四个层次
    干货:实现数据可视化的几个工具选择(工具+编程语言)
    案例分享|某医药集团的BI建设案例
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11173653.html
Copyright © 2020-2023  润新知