http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4918
昨天的排位,最初我还以为思维题,然后队友说状压DP,直接放弃,赛后看了队友的代码,在搜下网上的,发现队友的代码居然是最短的,膜拜啊~~~~~~~
思路是队友 A.L.的
dp[s]=min(dp[s],dp[s‘]+1)
其中s‘可以由s通过一次正着剪指甲或者反着剪指甲达到
至于内层循环,0-m,是因为----从剪指甲刀的最后一位看,最后一位从0移动到m,足够所有情况了
#include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <iostream> #include <iomanip> #include <cmath> #include <map> #include <set> #include <queue> using namespace std; #define ls(rt) rt*2 #define rs(rt) rt*2+1 #define ll long long #define ull unsigned long long #define rep(i,s,e) for(int i=s;i<e;i++) #define repe(i,s,e) for(int i=s;i<=e;i++) #define CL(a,b) memset(a,b,sizeof(a)) #define IN(s) freopen(s,"r",stdin) #define OUT(s) freopen(s,"w",stdout) const ll ll_INF = ((ull)(-1))>>1; const double EPS = 1e-8; const int MAXN = 22; const int INF =0x3f3f3f3f3f3f3f3f; int dp[1<<MAXN]; int main() { //IN("zoj3675.txt"); int up,down; int n,m; char str[MAXN]; while(~scanf("%d",&n)) { scanf("%s%d",str,&m); int len=strlen(str); up=down=0; for(int i=0;i<len;i++) { if(str[i]=='*') { up+=(1<<i); down+=(1<<(n-i-1)); } } CL(dp,INF); dp[(1<<m)-1]=0; for(int i=(1<<m)-1;i>=0;i--) for(int j=0;j<m;j++) { dp[i&~(up<<j)]=min(dp[i&~(up<<j)],dp[i]+1); dp[i&~(down<<j)]=min(dp[i&~(down<<j)],dp[i]+1); } printf("%d\n",dp[0]>=INF?-1:dp[0]); } return 0; }
时间: 2024-10-25 00:44:29