Start a blog, first talk about the code to achieve:
After every game, there must be summarized in the wrong habits.
Error Summary:
1. Read: scanf ( "% c") will read a carriage return and spaces, but cin will not.
2. to the input of the two-dimensional array, not the m, n confused, can cause serious program error, ranging from Wrong Answer, while in Run Time Error.
Note the DFS border issues.
After the match the game content and reflection
Problem 1
1339. disease (bacteria) (Standard IO)
Time limit: 1000 MS space constraints: 131072 KB specific restrictionsGoto ProblemSet
Fortuna OJ project
Author: moreD , RD ; Cooperation: Twilight , McHobby
by the CodeIgniter / Bootstrap drive
Glyphicons provided icons
Ideas: violent search.
Code: (TLE Error Code 22.2 points)
#include <cstdio> #include <iostream> #define rr register using namespace std; int n,d,k,a[1000][16],ans1=0,ans=0; bool c[16]; int max(int a,int b) { if(a>b) return a; return b; } void dfs(int i,bool xz,int jun,int sum) { bool tmp[16],temp=jun; for(rr int j=1;j<=15;j++) tmp[j]=c[j]; if(jun>k) return; if(i>n) { ans=max(ans,sum); return; } if(xz) { for(rr int j=1;j<=k;j++) if(a[i][j] && a[i][j]!=c[j]) { c[j]=a[i][j]; jun++; } if(jun>k) { for(rr int j=1;j<=15;j++) c[j]=tmp[j]; jun=temp; } else sum++; } dfs(i+1,true,jun,sum); dfs(i+1,false,jun,sum); } int main() { int tmp; scanf("%d %d %d",&n,&d,&k); for(rr int i=1;i<=n;i++) { scanf("%d",&a[i][0]); if(a[i][0]==0) { ans1++; i--; n--; } if(a[i][0]>k) { i--; n--; continue; } for(rr int j=1;j<=a[i][0];j++) { scanf("%d",&tmp); a[i][tmp]=true; } } dfs(1,true,0,0); dfs(1,false,0,0); printf("%d",ans); }
After the positive solution attached (I have not yet submitted test code)
Problem 2
1340. jumpcow (cow jump) (Standard IO)
Time limit: 1000 MS space constraints: 131072 KB specific restrictionsGoto ProblemSet
思路:动态规划。
附上代码:
(TLE错误代码20分DFS)
#include <cstdio> #include <iostream> #define rr register using namespace std; int p,a[150002],ans; int max(int a,int b) { if(a>b) return a; return b; } void dfs(rr int i,bool xz,bool zf,int sum) { if(i>p) { ans=max(ans,sum); return; } if(xz) { if(zf) sum+=a[i]; else sum-=a[i]; zf=!zf; } dfs(i+1,true,zf,sum); dfs(i+1,false,zf,sum); } int main() { scanf("%d",&p); for(rr int i=1;i<=p;i++) scanf("%d",&a[i]); dfs(1,true,true,0); dfs(1,false,true,0); printf("%d",ans); }
正解(AC100分DP)
#include <cstdio> #define rr register int max(int a,int b) { if(a>b) return a; return b; } int main() { int p,a[150001],f[150001][2]; scanf("%d",&p); for(rr int i=1;i<=p;i++) scanf("%d",&a[i]); f[1][0]=a[1]; for(rr int i=1;i<=p;i++) { f[i][0]=max(f[i-1][1]+a[i],f[i-1][0]); f[i][1]=max(f[i-1][0]-a[i],f[i-1][1]); } printf("%d",f[p][0]); }
Problem 3
思路:搜索(DFS,BFS均可)
代码实现:(DFS100分)
#include <cstdio> #include <iostream> #include <cstring> using namespace std; int n,m; char a[53][53]; bool bj[53][53]; int xi[]={0,0,-1,1}; int yi[]={1,-1,0,0}; void dfs(int x,int y) { bj[x][y]=true; for(int i=0;i<4;i++) { int x1=x+xi[i]; int y1=y+yi[i]; if(x1<=n && x1>0 && y1<=m && y1>0 && bj[x1][y1]==false && a[x1][y1]>=a[x][y]){ dfs(x1,y1); } }} int main() { int k=0; scanf("%d",&k); while(k--) { memset(bj,false,sizeof bj); scanf("%d %d",&n,&m); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) cin>>a[i][j]; } int ans=0; for(char c='a';c<='z';c++){ for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(bj[i][j]==false && a[i][j]==c) { dfs(i,j); ans++; } } } } printf("%d\n",ans); } }
Problem 4
思路:最小生成树
代码实现:Unknow.