博客
关于我
luogu P7262 Get Your Wish
阅读量:637 次
发布时间:2019-03-14

本文共 2613 字,大约阅读时间需要 8 分钟。

为了解决这个问题,我们需要模拟话筒内部水滴在新的重力方向下的流动情况。我们需要检查是否有水滴会流到关键电子元件上,如果有的话,话筒就会坏掉。

方法思路

  • 读取输入数据:首先读取话筒的大小和重力方向,然后读取话筒内部的状态矩阵。
  • 记录水滴位置:遍历状态矩阵,记录所有水滴的位置。
  • 模拟水滴流动:根据重力方向,模拟每个水滴的流动路径,检查是否会流到关键电子元件上。
  • 判断结果:如果有任何水滴流到关键电子元件上,输出"GG",否则输出"OK"。
  • 解决代码

    #include 
    #include
    #include
    #include
    #include
    #define LL long longusing namespace std;int n, m;char c;int x, y;char a[1002][1002];int b[1002][2]; // b[k][0]是i,b[k][1]是jint tot = 0;bool judge = true;void simulate() { if (c == 'v') { // 下方向,水滴向下流动 for (int k = 1; k <= tot; ++k) { int i = b[k][0]; int j = b[k][1]; int di = i + 1; while (di <= n && a[di][j] != 'x') { ++di; } if (a[di][j] == 'x' || di > n) { judge = false; return; } } } else if (c == '^') { // 上方向,水滴向上流动 for (int k = 1; k <= tot; ++k) { int i = b[k][0]; int j = b[k][1]; int di = i - 1; while (di >= 1 && a[di][j] != 'x') { --di; } if (a[di][j] == 'x' || di < 1) { judge = false; return; } } } else if (c == '>') { // 右方向,水滴向右流动 for (int k = 1; k <= tot; ++k) { int i = b[k][0]; int j = b[k][1]; int dj = j + 1; while (dj <= m && a[i][dj] != 'x') { ++dj; } if (a[i][dj] == 'x' || dj > m) { judge = false; return; } } } else if (c == '<') { // 左方向,水滴向左流动 for (int k = 1; k <= tot; ++k) { int i = b[k][0]; int j = b[k][1]; int dj = j - 1; while (dj >= 1 && a[i][dj] != 'x') { --dj; } if (a[i][dj] == 'x' || dj < 1) { judge = false; return; } } }}int main() { cin >> n >> m >> c; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cin >> a[i][j]; if (a[i][j] == 'o') { tot++; b[tot][0] = i; b[tot][1] = j; } } } if (tot > 0) { simulate(); } if (judge) { cout << "OK" << endl; } else { cout << "GG" << endl; } return 0;}

    代码解释

  • 读取输入:使用标准输入读取n、m和重力方向c,然后读取n行m列的状态矩阵。
  • 记录水滴位置:遍历状态矩阵,记录所有水滴的位置到数组b中。
  • 模拟流动:根据重力方向,分别处理水滴向下、向上、向右、向左流动的情况,检查是否有水滴流到关键电子元件上。
  • 判断结果:如果有任何水滴流到关键电子元件上,设置标志位judge为false,否则保持为true。最后根据标志位输出结果。
  • 转载地址:http://wpvoz.baihongyu.com/

    你可能感兴趣的文章
    npm install CERT_HAS_EXPIRED解决方法
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install 权限问题
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>