A
POJ 1321
acwing链接
#include <iostream>
#include <cstring>
using namespace std;
int n,k;
char map[90][90];
int cnt;
int l[90];
//每一行选择放还是不放
//若放 则遍历到可以放置的位置 进入下一层
//不放直接进入下一层
void dfs(int u,int z){
if (n-u+z<k) return ;
//剪枝 如果已经放了的加上最大可放数小于k 那么可以跳过了
if (z==k) {
cnt++;
return ;
}
if (u==n) return ;
for (int i=0;i<n;i++){
if (map[u][i]=='#'&&l[i]==0){
l[i]=1;
dfs(u+1,z+1);
l[i]=0;
//dfs(u+1,z);
}
}
dfs(u+1,z);
}
int main (){
while (1){
scanf ("%d%d",&n,&k);
if (n==-1) break;
for (int i=0;i<n;i++){
for (int j=0;j<n;j++) cin>>map[i][j];
}
cnt=0;
memset(l,0,sizeof l);
dfs(0,0);
cout<<cnt<<endl;
}
return 0;
}
B
poj2251
acwing链接
bfs模板题 只是换成三维了而已
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#define f first
#define s second
using namespace std;
char map[110][110][110];
int l,n,m;
bool mps[110][110][110];
int d[110][110][110];
int ls1,ns1,ms1,ls2,ns2,ms2;
int bfs(){
queue <pair<pair<int,int> ,int> > q;
q.push({{ls1,ns1},ms1});
while (q.size()){
pair<pair<int,int> ,int> t=q.front();
q.pop();
int dx[]={1,-1,0,0,0,0},dy[]={0,0,1,-1,0,0},dz[]={0,0,0,0,1,-1};
for (int i=0;i<6;i++){
int x=t.f.f+dx[i],y=t.f.s+dy[i],z=t.s+dz[i];
if (x>=0&&x<l&&y>=0&&y<n&&z>=0&&z<m&&d[x][y][z]==-1&&(map[x][y][z]=='.'||map[x][y][z]=='E')){
d[x][y][z]=d[t.f.f][t.f.s][t.s]+1;
q.push({{x,y},z});
}
}
}
return d[ls2][ns2][ms2];
}
int main (){
while (1){
cin>>l>>n>>m;
if (l==0) break;
memset(d,-1,sizeof d);
for (int i=0;i<l;i++){
for (int j=0;j<n;j++){
for (int k=0;k<m;k++){
cin>>map[i][j][k];
if (map[i][j][k]=='S') {
ls1=i,ns1=j,ms1=k;
d[i][j][k]=0;
}
if (map[i][j][k]=='E'){
ls2=i,ns2=j,ms2=k;
}
}
}
//cout<<l<<endl;
}
int t=bfs();
if (t!=-1) printf ("Escaped in %d minute(s).\n",t);
else cout<<"Trapped!"<<endl;
}
return 0;
}
C
poj3278
acwing链接
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
int x,k;
int d[100010];
int bfs(){
queue <int> q;
q.push(x);
memset(d,-1,sizeof d);
d[x]=0;
while (q.size()){
int t=q.front();
q.pop();
int dx[]={1,-1,t};
for (int i=0;i<3;i++){
int a=t+dx[i];
if (a>=0&&a<=100000&&d[a]==-1){
q.push(a);
d[a]=d[t]+1;
}
}
}
return d[k];
}
int main (){
scanf ("%d%d",&x,&k);
int t;
t=bfs();
cout<<t;
return 0;
}
D
poj3279
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
int n,m;
int map[20][20],ans[20][20];
void turn(int x,int y){
int dx[]={0,1,-1,0,0},dy[]={0,0,0,1,-1};
for (int i=0;i<5;i++){
int a=x+dx[i],b=y+dy[i];
if (a>=0&&a<n&&b>=0&&b<m){
map[a][b]=(map[a][b]+1)%2;
//ans[a][b]=1;
}
}
}
bool check(){
for (int i=n-1;i<n;i++){
for (int j=0;j<m;j++){
if (map[i][j]==1) return false;
}
}
return true;
}
int main (){
cin>>n>>m;
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
scanf ("%d",&map[i][j]);
}
}
int mins=890;
int ans2[20][20];
for(int i=0;i<1<<n;i++){
int t=i;
int u=0;
int cnt=0;
int g[20][20];
int g2[20][20];
memcpy(g,map,sizeof map);
memcpy(g2,ans,sizeof g2);
while (t){
if (t&1) {
turn(0,u);
cnt++;
//cout<<u<<' ';
ans[0][u]=1;
}
u++;
t>>=1;
}
//cout<<endl;
for (int i=1;i<n;i++){
for (int j=0;j<m;j++){
if (map[i-1][j]==1) {
turn(i,j);
ans[i][j]=1;
cnt++;
//cout<<i<<j<<' ';
}
//cout<<endl;
}
}
//cout<<endl;
if (check()) {
//mins=min(mins,cnt);
if (mins>cnt){
mins=cnt;
//cout<<cnt<<' ';
memcpy(ans2,ans,sizeof ans);
}
}
memcpy(map,g,sizeof g);
memcpy(ans,g2,sizeof g2);
}
if (mins<890) {
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
cout<<ans2[i][j]<<' ';
}
cout<<endl;
}
}
else printf ("IMPOSSIBLE\n");
//cout<<mins;
return 0;
}
E
poj1426
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
unsigned long long ans;
int n,p;
void dfs(unsigned long long x,int k){
if (p==1) return ;
if (x%n==0){
p=1;
//ans=x;
//cout<<x<
printf ("%llu\n",x);
return ;
}
if (k==19) return ;
dfs(x*10,k+1);
dfs(x*10+1,k+1);
}
int main (){
while(1){
scanf ("%d",&n);
if (!n) break;
p=0;
dfs(1,0);
//cout<<ans<<endl;
}
return 0;
}
F
poj3126
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
int a[10000],cnt;
bool st[10000];
int x,y;
int d[10009];
bool lj[10000];
void get_prime(int n){
for (int i=2;i<=n;i++){
if (!st[i]) a[cnt++]=i;
for (int j=0;a[j]<=n/i;j++){
st[a[j]*i]=1;
if (i%a[j]==0) break;
}
}
}
int bfs(){
memset(d,-1,sizeof d);
d[x]=0;
queue <int> q;
q.push(x);
while (q.size()){
int t=q.front();
q.pop();
int dx[]={0,1,2,3};
int num[2000];
int nums=0;
for (int i=0;i<4;i++){
//if (t==3739)cout<<'\n';
for (int j=0;j<10;j++){
int u=t;
if (i==0) {
u=t/10*10+j;
}
if (i==1) u=(t/100)*100+j*10+u%10;
if (i==2) u=(t-(t/100%10)*100)+j*100;
if (i==3&&j>=1) u=j*1000+u%1000;
//if (t==3739)cout<<u<<' ';
if (u>1000&&u<10000&&u!=t){
if(!st[u]){
num[nums++]=u;
}
}
}
}
for (int i=0;i<nums;i++){
if (d[num[i]]!=-1) {
if (d[num[i]]>d[t]+1) {
d[num[i]]=d[t]+1;
q.push(num[i]);
}
}
else {
d[num[i]]=d[t]+1;
q.push(num[i]);
}
}
}
return d[y];
}
int main (){
int m;
cin>>m;
get_prime(10000);
while (m--){
scanf ("%d %d",&x,&y);
int z=bfs();
if (z!=-1)cout<<z<<endl;
else printf ("Impossible\n");
}
return 0;
}
G
poj3087
//我以为是个搜索题 结果是个模拟题
//应以为戒 思考问题要全面
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
// #include <unordered_set>
// #include <unordered_map>
//poj好像不支持c++11
#define f first
#define s second
using namespace std;
int t,n;
string a,b,ans;
int cs;
int main (){
cin>>t;
int z=0;
while (t--){
z++;
scanf ("%d",&n);
cin>>a>>b>>ans;
string u;
for (int i=0;i<n;i++) {
u+=b[i];
u+=a[i];
}
int i;
string t=u;
int p=1;
for (i=1;t!=ans;i++){
string ss=t;
string a1=ss.substr(0,n),b1=ss.substr(n);
string s2;
for (int j=0;j<n;j++){
s2+=b1[j];
s2+=a1[j];
}
if (s2==u) {
p=0;
break;
}
t=s2;
}
if (p) printf ("%d %d\n",z,i);
else printf ("%d %d\n",z,-1);
}
return 0;
}