有墙情况的8皇后问题。。
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 #include <cstring> 6 #include <queue> 7 #include <map> 8 #define ll long long 9 #define out(a) printf("%d",a) 10 #define writeln printf(" ") 11 const int N=1e5+50; 12 using namespace std; 13 int n; 14 char ch[50][50]; 15 bool vis[50][50]; 16 int cnt,num; 17 int read() 18 { 19 int s=0,t=1; char c; 20 while (c<'0'||c>'9'){if (c=='-') t=-1; c=getchar();} 21 while (c>='0'&&c<='9'){s=s*10+c-'0'; c=getchar();} 22 return s*t; 23 } 24 ll readl() 25 { 26 ll s=0,t=1; char c; 27 while (c<'0'||c>'9'){if (c=='-') t=-1; c=getchar();} 28 while (c>='0'&&c<='9'){s=s*10+c-'0'; c=getchar();} 29 return s*t; 30 } 31 bool check(int x,int y) 32 { 33 int a=x-1; 34 bool f=false,flag=false; 35 if (vis[x][y]) return false; 36 while (a>=0){ 37 if (ch[a][y]=='X') f=true; 38 if (vis[a][y]) flag=true; 39 if (flag&&!f) { 40 return false;} 41 a--; 42 } 43 f=flag=false; 44 a=y-1; 45 while (a>=0){ 46 if (ch[x][a]=='X') f=true; 47 if (vis[x][a]) flag=true; 48 if (flag&&!f) { 49 return false;} 50 a--; 51 } 52 return true; 53 } 54 void dfs(int dep,int sum) 55 { 56 int x_,y_; 57 if (dep==n*n){ 58 //if (n==2) out(233),writeln; 59 if (sum>cnt) cnt=sum; 60 return; 61 } 62 else{ 63 x_=dep/n; y_=dep%n; 64 if (ch[x_][y_]== '.'&&check(x_,y_)){ 65 //if (n==2) out(x_),out(y_),writeln; 66 vis[x_][y_]=true; 67 dfs(dep+1, sum+1); 68 vis[x_][y_]=false; 69 } 70 dfs(dep+1,sum); 71 } 72 } 73 int main() 74 { 75 while (~scanf("%d",&n)){ 76 if (n==0) break; num=0; 77 memset(vis,false,sizeof(vis)); 78 for (int i=0;i<n;i++) 79 for (int j=0;j<n;j++){ 80 cin>>ch[i][j]; 81 if (ch[i][j]=='.') num++; 82 } 83 cnt=0; 84 dfs(0,0); 85 out(cnt); writeln; 86 } 87 return 0; 88 }
如果选一个数,那么左右相邻的数和上下一整行的数不能选,求最大和。
考虑先求出每行的最大值,然后求出每列的最大值.
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 #include <cstring> 6 #include <queue> 7 #include <map> 8 #define ll long long 9 #define out(a) printf("%d",a) 10 #define writeln printf(" ") 11 const int N=1e5+50; 12 using namespace std; 13 int n,m,x; 14 int ans; 15 int f[N],dp[N]; 16 int read() 17 { 18 int s=0,t=1; char c; 19 while (c<'0'||c>'9'){if (c=='-') t=-1; c=getchar();} 20 while (c>='0'&&c<='9'){s=s*10+c-'0'; c=getchar();} 21 return s*t; 22 } 23 ll readl() 24 { 25 ll s=0,t=1; char c; 26 while (c<'0'||c>'9'){if (c=='-') t=-1; c=getchar();} 27 while (c>='0'&&c<='9'){s=s*10+c-'0'; c=getchar();} 28 return s*t; 29 } 30 int main() 31 { 32 while (~scanf("%d%d",&n,&m)){ 33 memset(dp,0,sizeof(dp)); 34 for (int i=1;i<=n;i++){ 35 for (int j=1;j<=m;j++) { 36 x=read(); 37 f[j]=max(f[j-1],f[j-2]+x); 38 } 39 dp[i]=max(dp[i-1],dp[i-2]+f[m]); 40 } 41 out(dp[n]); writeln; 42 } 43 return 0; 44 //f[3]=7 f[4]=7; f[5]= 20 f[6]=20 f[7]=20 45 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 #include <cstring> 6 #include <queue> 7 #include <map> 8 #define ll long long 9 #define out(a) printf("%d",a) 10 #define writeln printf(" ") 11 const int N=1e5+50; 12 using namespace std; 13 int n,m,x; 14 int ans,maxn; 15 int cost[N]; 16 char s[N]; 17 double num,v[N],f[N]; 18 int read() 19 { 20 int s=0,t=1; char c; 21 while (c<'0'||c>'9'){if (c=='-') t=-1; c=getchar();} 22 while (c>='0'&&c<='9'){s=s*10+c-'0'; c=getchar();} 23 return s*t; 24 } 25 ll readl() 26 { 27 ll s=0,t=1; char c; 28 while (c<'0'||c>'9'){if (c=='-') t=-1; c=getchar();} 29 while (c>='0'&&c<='9'){s=s*10+c-'0'; c=getchar();} 30 return s*t; 31 } 32 int main() 33 { 34 n=read(); 35 while (n--){ 36 maxn=0; 37 scanf("%lf",&num); m=read(); 38 for (int i=1;i<=m;i++) 39 cost[i]=read(),scanf("%lf",&v[i]),maxn+=cost[i]; 40 //out(maxn); writeln; 41 memset(f,0,sizeof(f)); 42 f[0]=1; 43 for (int i=1;i<=m;i++) 44 for (int j=maxn;j>=cost[i];j--) 45 f[j]=max(f[j],f[j-cost[i]]*(1-v[i])); 46 for (int i=maxn;i>=0;i--){ 47 //cout<<f[i]<<endl; 48 if (f[i]>1-num){ 49 out(i); writeln; break;} 50 } 51 } 52 return 0; 53 }