cf 106d Treasure Island
来源:互联网 发布:商场数据分析 编辑:程序博客网 时间:2024/06/05 19:16
点击打开链接
题意:
给你一个地图,地图上 ‘.‘表示能走的点。’#' 表示不能走的点,
然后给你最多26个出发点,然后给你走的方法, 如果按照给的方法,完全可行(不超界,不遇见'#'),就是可行点,
最后把所有可行点按升序输出,没有输出no。
题解:
我算了一下复杂度,感觉大暴力可能过,于是一个一个位移,跑了快60个测试点,,,,TLE。
就在纠结怎么优化,后来一次走一行或一列,就不会超时了,这样的话,要记录每一行和每一列#数量 的前缀和,
如果能走,前缀和之差一定是0.
#include<bits/stdc++.h>using namespace std;const int maxn=1010;const int maxk=1e5+100;const int inf=0xfffffff;int n,m,k,cnt=0;char ma[maxn][maxn];int ps[maxn][maxn],pr[maxn][maxn];int ans[30];struct node{ char d; int s;}a[maxk];int main(){ memset(ma,'#',sizeof(ma)); cin>>n>>m; for(int i=1;i<=n;++i) { scanf("%s",ma[i]+1),ma[i][m+1]='#'; for(int j=1;j<=m;++j){ ps[i][j]=ps[i][j-1]; pr[i][j]=pr[i-1][j]; if(ma[i][j]=='#') ps[i][j]++,pr[i][j]++; } } cin>>k; for(int i=1;i<=k;++i) {getchar(); scanf("%c %d",&a[i].d,&a[i].s); } for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ if(ma[i][j]>='A'&&ma[i][j]<='Z'){ int tx=i,ty=j,f=1,x=i,y=j; for(int ia=1;ia<=k;++ia){ if(a[ia].d=='N') tx-=a[ia].s; else if(a[ia].d=='S') tx+=a[ia].s; else if(a[ia].d=='W') ty-=a[ia].s; else ty+=a[ia].s; if(tx<1||tx>n||ty<1||ty>m||ma[tx][ty]=='#'){ f=0;break; } if(y==ty){ if(pr[x][y]-pr[tx-1][ty]!=0){ f=0;break; } } if(x==tx){ if(ps[x][y]-ps[tx][ty-1]!=0){ f=0;break; } } x=tx,y=ty; }if(f) ans[cnt++]=ma[i][j]; } } } if(!cnt){ printf("no solution\n");return 0; } sort(ans,ans+cnt); for(int i=0;i<cnt;++i)printf("%c",ans[i]); puts(""); return 0;}
阅读全文
0 0
- cf 106d Treasure Island
- Codeforces 106 D Treasure Island
- Codeforces 106D Treasure Island 预处理前缀和+暴力(水
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 106D Treasure Island【思维+二维前缀和】
- CF-63D - Dividing Island(水题)
- cf 540D D. Bad Luck Island 概率dp
- (二分,BFS).Treasure Island
- Cf Round #301 (Div. 2) D. Bad Luck Island
- cf#301-D - Bad Luck Island-概率dp(水 )
- CF 540D. Bad Luck Island 概率dp
- Gym 100971A Treasure Island
- CF#494C Treasure
- CF 282C Treasure
- CF# 301 D Bad Luck Island(概率dp+记忆化)
- CF #301 (Div. 2) D. Bad Luck Island (概率期望dp)
- CF 2014 ACM-ICPC Vietnam National Second Round D. Treasure Box
- HDU 2416 Treasure of the Chimp Island
- C++栈与队列基本操作
- 【Kotlin从入门到深坑】之返回和跳转
- 安信可-A7模块——GPRS基本AT命令
- *后缀数组
- 测试05-02
- cf 106d Treasure Island
- 巩朋:我的算法学习之路
- Keil SIGABRT Abnormal termination
- spring的AOP(四)----Spring AOP 编程(Advisor)
- 【PostgreSQL-9.6.3】函数(3)--日期和时间函数
- 《挑战程序设计竞赛》阅读笔记一 之 ALDS1_1_C Prime Numbers
- 学习淘淘商城第八十一课(SSO 系统分析)
- RecyclerView.ItemDecoration的使用
- python——堡垒机