主题链接:点击打开链接

意甲冠军:

特定n*m矩阵

# 是墙 . 和字母是平地

最多有26个字母(不反复出现)

以下k个指令。

每一个指令代表移动的方向和步数。

若以某个字母为起点,依次运行全部的指令,不论什么过程都不会撞到墙或走出地图。则这个字母合法。

按字典序输出全部合法的字母。若没有字母合法则输出' no solution'

预处理一下前缀和然后暴力。

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <vector>
  7. #include <map>
  8. using namespace std;
  9. #define N 1005
  10. vector<char>ans;
  11. struct node{
  12. int x, y;
  13. char c;
  14. void put(){printf("(%d,%d) : %c\n", x, y, c);}
  15. }a[30];
  16. int step[4][2] = {-1,0, 1,0, 0,-1, 0,1};
  17. int work[100005][2];
  18. char s[N];
  19. int mp[N][N], n, m, k, top, h[N][N], l[N][N];
  20. bool okh(int H, int x, int y){return h[H][y]-h[H][x-1] == 0;}
  21. bool okl(int L, int x, int y){return l[L][y]-l[L][x-1] == 0;}
  22. bool ok(int x, int y, int i, int j){
  23. if(x>i)swap(x,i); if(y>j)swap(y,j);
  24. if(x==i)
  25. return okh(x, y, j);
  26. else
  27. return okl(y, x, i);
  28. }
  29. bool inmap(int x, int y){return 1<=x&&x<=n&&1<=y&&y<=m;}
  30. bool judge(int x, int y){
  31. for(int i = 0; i < k; i++) {
  32. int ux = step[work[i][0]][0] * work[i][1] + x, uy = step[work[i][0]][1] *work[i][1] + y;
  33. if(!inmap(ux,uy))return false;
  34. if(!ok(x, y, ux, uy))return false;
  35. x = ux; y = uy;
  36. }
  37. return true;
  38. }
  39. void debug(){for(int i = 0; i < top; i++)a[i].put();}
  40. void solve(){
  41. // debug();
  42. ans.clear();
  43. for(int i = 0; i < top; i++)
  44. if(judge(a[i].x, a[i].y))
  45. ans.push_back(a[i].c);
  46. if(ans.size()==0){
  47. puts("no solution");
  48. return ;
  49. }
  50. sort(ans.begin(), ans.end());
  51. for(int i = 0; i < ans.size(); i++)printf("%c",ans[i]);
  52. puts("");
  53. }
  54. void input(){
  55. top = 0;
  56. memset(mp, 0, sizeof mp);
  57. memset(h, 0, sizeof h);
  58. memset(l, 0, sizeof l);
  59. for(int i = 1; i <= n; i++){
  60. scanf("%s", s+1);
  61. for(int j = 1; j <= m; j++)
  62. {
  63. if(s[j]=='#')
  64. {
  65. mp[i][j] = 1;
  66. h[i][j] = 1;
  67. l[j][i] = 1;
  68. }
  69. else if('A'<=s[j] && s[j]<='Z'){
  70. a[top].x = i; a[top].y = j; a[top].c = s[j];
  71. top++;
  72. }
  73. }
  74. }
  75. for(int i = 1; i <= n; i++)
  76. for(int j = 1; j <= m; j++)
  77. h[i][j] += h[i][j-1];
  78. for(int i = 1; i <= m; i++)
  79. for(int j = 1; j <= n; j++)
  80. l[i][j] += l[i][j-1];
  81. scanf("%d",&k);
  82. for(int i = 0; i < k; i++){
  83. scanf("%s %d", s, &work[i][1]);
  84. if(s[0] == 'N') work[i][0] = 0;
  85. else if(s[0]=='S') work[i][0] = 1;
  86. else if(s[0]=='W') work[i][0] = 2;
  87. else work[i][0] = 3;
  88. }
  89. }
  90. int main(){
  91. while(~scanf("%d %d",&n,&m)){
  92. input();
  93. solve();
  94. }
  95. return 0;
  96. }

版权声明:本文博主原创文章。博客,未经同意不得转载。

Codeforces 106D Treasure Island 预处理前缀+暴力(水的更多相关文章

  1. [Codeforces 1214D]Treasure Island(dfs)

    [Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...

  2. CodeForces 838A Binary Blocks(前缀和)题解

    题意:给你个n*m的矩阵,要求你找到一个k,k > 1,使得矩阵可以分为很多k * k的小正方形,然后进行操作把每个小正方形都变为0或1,问你怎样使操作数最小. 思路:随便暴力不可取,显然你每次 ...

  3. Gym 100971A Treasure Island BFS 思维题

    A - Treasure Island Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  4. [Codeforces 1201D]Treasure Hunting(DP)

    [Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...

  5. D. Treasure Island

    D. Treasure Island dfs大法好== 写半天bfs疯狂MLE dfs标记掉路上的一些点 然后再跑一遍dfs #include<bits/stdc++.h> using n ...

  6. Treasure Island DFS +存图

    All of us love treasures, right? That's why young Vasya is heading for a Treasure Island. Treasure I ...

  7. Codeforces Round #280 (Div. 2) A B C 暴力 水 贪心

    A. Vanya and Cubes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. Karen and Coffee CodeForces - 816B (差分数组+预处理前缀和)

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

  9. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) A B C D 暴力 水 二分 几何

    A. Vicious Keyboard time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. JS错误记录 - 微博发布

    <style> *{ margin: 0; padding: 0;} #ul1{ width: 400px; height: 400px; border: 1px solid #000; ...

  2. POJ 2376 Cleaning Shifts 区间覆盖问题

    http://poj.org/problem?id=2376 题目大意: 给你一些区间的起点和终点,让你用最小的区间覆盖一个大的区间. 思路: 贪心,按区间的起点找满足条件的并且终点尽量大的. 一开始 ...

  3. Instant Client 配置

    Instant Client Download 选择  Instant Client for Microsoft Windows (32-bit)  由于PL/SQL Developer 不支持64b ...

  4. python3 turtle画正方形、矩形、正方体、五角星、奥运五环

    python3 环境 turtle模块 分别画出 正方形.矩形.正方体.五角星.奥运五环 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:H ...

  5. DC针对pipeline的优化

    set_optimize_register    true compile  -ultra 调整pipleline各级的组合逻辑,使得各级组合逻辑的延迟跟接近 对非pipeline进行优化: regi ...

  6. 数据结构-堆实现优先队列(java)

    队列的特点是先进先出.通常都把队列比喻成排队买东西,大家都非常守秩序,先排队的人就先买东西. 可是优先队列有所不同,它不遵循先进先出的规则,而是依据队列中元素的优先权,优先权最大的先被取出. 这就非常 ...

  7. php课程 10-35 php实现文件上传的注意事项是什么

    php课程 10-35 php实现文件上传的注意事项是什么 一.总结 一句话总结:记得限制大小和类型,还有就是用move_uploaded_file($sfile,$dfile);函数把上传到php临 ...

  8. 快速理解Java中的五种单例模式(转)

    解法一:只适合单线程环境(不好) package test; /** * @author xiaoping * */ public class Singleton { private static S ...

  9. HDU 1406 完数 因子的和

    http://acm.hdu.edu.cn/showproblem.php?pid=1406 完数的定义:如果一个大于1的正整数的所有因子之和等于它的本身,则称这个数是完数,比如6,28都是完数:6= ...

  10. hibernate---java.lang.UnsupportedOperationException: The user must supply a JDBC connection

        在配置hibernate时.运行代码时一直抛错: Exception in thread "main" java.lang.UnsupportedOperationExce ...