#include<iostream>#include<vector>#include<queue>#include<cstring>usingnamespace std;constint N =1000;int n, m;typedef pair<int,int> PII;typedef pair<int, PII> PIII;int f[N][N];bool st[N][N];int dx[12]={-2,-2,-2,-2,-1,-1,1,1,2,2,2,2}, dy[12]={-2,-1,1,2,-2,2,-2,2,-2,-1,1,2};intbfs(int a,int b){
queue<PIII> q; q.push({0,{a,b}});while(q.size()){auto t = q.front(); q.pop();int x = t.second.first, y = t.second.second;int d = t.first;if(x ==1&& y ==1)return d;if(st[x][y])continue;
st[x][y]=true;for(int i =0; i <12; i++){int x1 = x + dx[i], y1 = y + dy[i];if(x1 >0&& x1 <=20&& y1 >0&& y1 <=20&&!st[x1][y1]){if(x1 ==1&& y1 ==1)return d +1;
q.push({ d +1,{x1,y1}});}}}return-1;}intmain(){int x, y, a, b;
cin >> x >> y >> a >> b;
cout <<bfs(x,y)<< endl;memset(st,false,sizeof st);
cout <<bfs(a, b)<< endl;}
P1331 海战
用两个set分别存 行和列 然后再乘看是不是这么大
#include<iostream>#include<vector>#include<queue>#include<cstring>#include<set>usingnamespace std;constint N =1010;int n, m;typedef pair<int,int> PII;typedef pair<int, PII> PIII;
string f[N];int res =0;int dx[4]={-1,0,1,0}, dy[4]={0,1,0,-1};voiddfs(int x,int y,set<int>&t1,set<int>&t2){
res++;
f[x][y]='.';
t1.insert(x);
t2.insert(y);for(int i =0; i <4; i++){int a = dx[i]+ x, b = dy[i]+ y;if(a >=0&& a < n && b >=0&& b < m && f[a][b]=='#'){dfs(a, b, t1, t2);}}}intmain(){
cin >> n >> m;int ans =0;for(int i =0; i < n; i++)cin >> f[i];for(int i =0; i < n; i++){for(int j =0; j < m; j++){if(f[i][j]=='#'){
set<int> t1,t2;
res =0;
ans++;dfs(i, j, t1, t2);if(t1.size()* t2.size()!= res){printf("Bad placement.");return0;}}}}printf("There are %d ships.", ans);}
P1219 [USACO1.5] 八皇后 Checker Challenge
首先传入行 然后用三个数组表示列 正对角线 反对角线 然后判断是否用过
#include<iostream>#include<stack>#include<queue>#include<map>#include<cstring>#include<sstream>#include<algorithm>#include<vector>usingnamespace std;#defineintlonglongconstint N =50,inf=0x3f3f3f3f3f;typedef pair<int,int> PII;typedef pair<PII,int> PIII;int n, m;bool r[N],c[N],cd[N],cr[N];int f[N];int ans=3;int res;voiddfs(int x){if(x == n){
res++;if(ans >0){for(int i =0; i < n; i++){
cout << f[i]<<" ";}
cout << endl;
ans--;}return;}for(int i =1; i <= n; i++){if(!c[i]&&!cd[i + x]&&!cr[n - i + x]){
c[i]= cd[i + x]= cr[n - i + x]=true;
f[x]= i;dfs(x +1);
c[i]= cd[i + x]= cr[n - i + x]=false;
f[x]=0;}}}signedmain(){
cin >> n;dfs(0);
cout << res << endl;}
P1123 取数游戏
这题dfs时 用一个那么就应该把周围和自已全部设为true 然后dfs x>n时 就结束
#include<iostream>#include<stack>#include<queue>#include<map>#include<cstring>#include<sstream>#include<algorithm>#include<vector>usingnamespace std;#defineintlonglongconstint N =20,inf=0x3f3f3f3f3f;typedef pair<int,int> PII;typedef pair<PII,int> PIII;int n, m;int r[N],c[N],cd[N],cr[N];int f[N][N];int st[N][N];int ans ;voiddfs(int x,int y,int z){if(y> m){
y =1; x++;}if(x > n){
ans =max(z, ans);return;}if(st[x][y]==0){for(int i =-1; i <=1; i++){for(int j =-1; j <=1; j++){++st[x + i][y + j];}}dfs(x, y +2, z + f[x][y]);for(int i =-1; i <=1; i++){for(int j =-1; j <=1; j++){--st[x + i][y + j];}}}dfs(x, y +1, z);}signedmain(){int t; cin >> t;while(t--){
cin >> n >> m;for(int i =1; i <= n; i++){for(int j =1; j <= m; j++){
cin >> f[i][j];}}memset(st,0,sizeof st);
ans =0;dfs(1,1,0);
cout << ans << endl;}}
P1025 [NOIP2001 提高组] 数的划分
数的划分 首先肯定是像全排列差不多 但是需要考虑 次数 和剪纸
#include<iostream>#include<stack>#include<queue>#include<map>#include<cstring>#include<sstream>#include<algorithm>#include<vector>usingnamespace std;#defineintlonglongconstint N =410,inf=0x3f3f3f3f3f;typedef pair<int,int> PII;typedef pair<PII,int> PIII;int n, m;int x, y;int f[N][N];int dx[8]={-2,-1,1,2,2,1,-1,-2}, dy[8]={1,2,2,1,-1,-2,-2,-1};int ans =0;
vector<vector<int>> v;voiddfs(int sum,int t,int last ){if(t==m){if(sum == n)ans++;return;}for(int i = last; sum +(m - t)* i<= n; i++){dfs(sum + i, t +1, i);}}signedmain(){
cin >> n >> m;dfs(0,0,1);
cout << ans << endl;}
P1443 马的遍历
还是一个bfs 存点 遍历当前能到的点
#include<iostream>#include<stack>#include<queue>#include<map>#include<cstring>#include<sstream>#include<algorithm>#include<vector>usingnamespace std;#defineintlonglongconstint N =410,inf=0x3f3f3f3f3f;typedef pair<int,int> PII;typedef pair<PII,int> PIII;int n, m;int x, y;int f[N][N];int dx[8]={-2,-1,1,2,2,1,-1,-2}, dy[8]={1,2,2,1,-1,-2,-2,-1};bool st[N][N];
vector<vector<int>> v;voiddfs(int a,int b){memset(f,-1,sizeof f);
f[a][b]=0;
queue<PIII> q; q.push({{ a,b },0});while(q.size()){auto t = q.front(); q.pop();int a2 = t.first.first, b2 = t.first.second;int d = t.second;for(int i =0; i <8; i++){int a1 = dx[i]+ a2, b1 = dy[i]+ b2;if(a1 >0&& a1 <= n && b1 >0&& b1 <= m && f[a1][b1]==-1){
q.push({{ a1,b1 },d+1});
f[a1][b1]= d +1;}}}}signedmain(){
cin >> n >> m;
cin >> x >> y;dfs(x, y);for(int i =1; i <= n; i++){for(int j =1; j <= m; j++){
cout << f[i][j]<<" ";}
cout << endl;}}
#include<iostream>#include<stack>#include<queue>#include<map>#include<cstring>#include<sstream>#include<algorithm>#include<vector>usingnamespace std;#defineintlonglongconstint N =50,inf=0x3f3f3f3f3f;int n, m;bool f[N];int res;signedmain(){
cin >> n;
m = n;int flag =1;while(flag){
m++;int c =0;for(int i =0; i < n; i++){
c =(c + m -1)%(2* n -i);if(c < n)break;if(i == n -1)flag =0;}}
cout << m << endl;}