Codeforces 106 D. Treasure Island(前缀和预处理)
来源:互联网 发布:vb.net 高级编程 pdf 编辑:程序博客网 时间:2024/06/06 08:39
Treasure Island
题目链接:Treasure Island
题意:给定n*m的矩阵, # 是墙 . 和字母是平地,最多有26个字母(不重复出现)
下面k个指令,每个指令指出移动的方向和步数。
若以某个字母为起点,依次执行所有的指令,任何过程都不会撞到墙或走出地图,则这个字母合法。
按字典序输出所有合法的字母。若没有字母合法则输出’ no solution’
思路:
先对地图先预处理出前缀和,
若
由于字母最多只有26个,因此可以直接暴力模拟
代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn=1e3+10;struct node{ int x,y; char c;} a[30];int work[maxn*100][2];int to[][2]= {-1,0,1,0,0,1,0,-1};int rs[maxn][maxn],cs[maxn][maxn];char s[maxn];int n,m,k,tot,ps;bool ok_range(int x,int y){ return x>=0&&x<n&&y>=0&&y<m;}bool ok_row(int x,int y,int ty){ if(ty<y) swap(ty,y); return rs[x][ty]-rs[x][y-1]==0;}bool ok_column(int y,int x,int tx){ if(tx<x) swap(tx,x); return cs[tx][y]-cs[x-1][y]==0;}bool judge(int x,int y,int tx,int ty){ if(ok_range(x,y)&&(ok_range(tx,ty))&&ok_row(x,y,ty)&&ok_column(y,x,tx)) return true; return false;}void input(){ scanf("%d%d",&n,&m); for(int i=0; i<n; ++i) { scanf("%s",s); for(int j=0; j<m; ++j) { if(s[j]>='A'&&s[j]<='Z') a[tot].x=i,a[tot].y=j,a[tot++].c=s[j]; else if(s[j]=='#') rs[i][j]=cs[i][j]=1; } } for(int i=0; i<n; ++i) for(int j=0; j<m; ++j) rs[i][j]+=rs[i][j-1]; for(int j=0; j<m; ++j) for(int i=0; i<n; ++i) cs[i][j]+=cs[i-1][j]; scanf("%d",&k); for(int i=0; i<k; ++i) { scanf("%s%d",s,&work[i][1]); if(s[0]=='N') work[i][0]=0; else if(s[0]=='S') work[i][0]=1; else if(s[0]=='E') work[i][0]=2; else work[i][0]=3; }}void solve(){ for(int i=0,j; i<tot; ++i) { int x=a[i].x,y=a[i].y; for(j=0; j<k; ++j) { int tx=to[work[j][0]][0]*work[j][1]+x,ty=to[work[j][0]][1]*work[j][1]+y; if(!judge(x,y,tx,ty)) break; x=tx,y=ty; } if(j==k) s[ps++]=a[i].c; } if(ps) { s[ps]='\0'; sort(s,s+ps); printf("%s\n",s); } else printf("no solution\n");}int main(){ input(); solve(); return 0;}
参考博客:
九野
阅读全文
1 0
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106D Treasure Island 预处理前缀和+暴力(水
- Codeforces 106D Treasure Island【思维+二维前缀和】
- Codeforces 106 D Treasure Island
- cf 106d Treasure Island
- [CodeForces 677D] Vanya and Treasure (二维树状数组+静态前缀最小值)
- Codeforces Round #294 (Div. 2) (D)前缀和+map
- Codeforces 846D Monitor(二维前缀和)
- Codeforces 854 D Jury Meeting(前缀和后缀)
- codeforces 846D (二分+二维前缀和)Monitor
- 【Goodbye2015】Codeforces 611D New Year and Ancient Prophecy【Dp+前缀和优化+预处理字符串】好题!好题!
- [高维前缀和] Codeforces 449D
- Codeforces 846C Four Segments【思维+预处理+前缀和枚举】
- CodeForces 540D Bad Luck Island (概率dp)
- CFgym:Treasure Island(搜索 & 思维)
- 预处理之前缀和
- CodeForces 546D Soldier and Number Game(求素因子+数学+前缀和)
- Codeforces 712D Memory and Scores(前缀和优化dp)
- 【Hibernate】(一)快速入门
- 透彻的掌握 Spring 中@transactional 的使用
- c++基础知识总结
- MySQL Oracle 别名
- 解决android7.0+ 自定义Toast时长,但是toast不显示问题
- Codeforces 106 D. Treasure Island(前缀和预处理)
- spring mvc InitBinder 遇到的坑接收不了参数
- Android设计模式之(3)----原型模式
- Krpano插件--event事件注释
- Bootstrap Table使用整理(一)
- TensorFlow 训练 MNIST 数据
- 配置表的处理方式
- ESXi对接iscsi存储操作指南
- Insertion Sort List