Codeforces 106 D. Treasure Island(前缀和预处理)
2017-08-16 11:08
344 查看
Treasure Island
题目链接:Treasure Island题意:给定n*m的矩阵, # 是墙 . 和字母是平地,最多有26个字母(不重复出现)
下面k个指令,每个指令指出移动的方向和步数。
若以某个字母为起点,依次执行所有的指令,任何过程都不会撞到墙或走出地图,则这个字母合法。
按字典序输出所有合法的字母。若没有字母合法则输出’ no solution’
思路:
先对地图先预处理出前缀和,ls[][]表示行的前缀和,cs[][]表示列的前缀和
若ls[x][ty]−ls[x][y−1]=0表明在第x行,可以从第y列直接走到第ty列。cs[][]同理
由于字母最多只有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; }
参考博客:
九野
相关文章推荐
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106 D Treasure Island
- Codeforces 106D Treasure Island 预处理前缀和+暴力(水
- codeforces 234C C. Weather(枚举+前缀后缀预处理)
- Codeforces 835C - Star sky 【预处理前缀和】
- Codeforces 835C Star sky【思维+暴力预处理二维前缀和】
- Codeforces 846C Four Segments【思维+预处理+前缀和枚举】