Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <fstream>
- using namespace std;
- ifstream f("sah.in");
- ofstream g("sah.out");
- int a[10][10],i1,j1,l=0,ii,ji,ok=0,x[100],p=0;
- int E[4]={-1,-2,1,-2};
- int V[4]={-1,2,1,2};
- int N[4]={-2,-1,-2,1};
- int S[4]={2,-1,2,1};
- int in(int i,int j)
- {
- x[l]=i*10+j;
- l++;
- }
- int sol(int i,int j)
- {
- if(i==i1&&j==j1) {ok=1;
- for(i=1;i<=8;i++)
- for(j=1;j<=8;j++)
- if(a[i][j]==1) a[i][j]=7;
- return 1;}
- else return 0;
- }
- int sol1(int i,int j)
- {
- if(i==i1&&j==j1) {a[i][j]=1; return 1;}
- else return 0;
- }
- int valid(int &i,int &j)
- {
- p++;
- if(i+N[0]==i1&&j+N[1]==j1) {i=i+N[0]; j=j+N[1]; return 1;}
- if(i+N[2]==i1&&j+N[3]==j1) {i=i+N[2]; j=j+N[3]; return 1;}
- if(i+S[0]==i1&&j+S[1]==j1) {i=i+S[0]; j=j+S[1]; return 1;}
- if(i+S[2]==i1&&j+S[3]==j1) {i=i+S[2]; j=j+S[3]; return 1;}
- if(i+E[0]==i1&&j+E[1]==j1) {i=i+E[0]; j=j+E[1]; return 1;}
- if(i+E[2]==i1&&j+E[3]==j1) {i=i+E[2]; j=j+E[3]; return 1;}
- if(i+V[0]==i1&&j+V[1]==j1) {i=i+V[0]; j=j+V[1]; return 1;}
- if(i+V[2]==i1&&j+V[3]==j1) {i=i+V[2]; j=j+V[3]; return 1;}
- if(i1<i||j1<j)
- {
- if(a[i+N[0]][j+N[1]]==0&&i+N[0]>=1&&j+N[1]>=1) {i=i+N[0]; j=j+N[1]; return 1;}
- else if(a[i+N[2]][j+N[3]]==0&&i+N[2]>=1&&j+N[3]<=8) {i=i+N[2]; j=j+N[3]; return 1;}
- else if(a[i+E[0]][j+E[1]]==0&&i+E[0]>=1&&j+E[1]>=1) {i=i+E[0]; j=j+E[1]; return 1;}
- else if(a[i+E[2]][j+E[3]]==0&&i+E[2]<=8&&j+E[3]>=1) {i=i+E[2]; j=j+E[3]; return 1;}
- else if(a[i+V[0]][j+V[1]]==0&&i+V[0]>=1&&j+V[1]<=8) {i=i+V[0]; j=j+V[1]; return 1;}
- else if(a[i+V[2]][j+V[3]]==0&&i+V[2]<=8&&j+V[3]<=8) {i=i+V[2]; j=j+V[3]; return 1;}
- else if(a[i+S[0]][j+S[1]]==0&&i+S[0]<=8&&j+S[1]>=1) {i=i+S[0]; j=j+S[1]; return 1;}
- else if(a[i+S[2]][j+S[3]]==0&&i+S[2]<=8&&j+S[3]<=8) {i=i+S[2]; j=j+S[3]; return 1;}
- }
- else{
- if(a[i+S[2]][j+S[3]]==0&&i+S[2]<=8&&j+S[3]<=8) {i=i+S[2]; j=j+S[3]; return 1;}
- else if(a[i+S[0]][j+S[1]]==0&&i+S[0]<=8&&j+S[1]>=1) {i=i+S[0]; j=j+S[1]; return 1;}
- else if(a[i+V[2]][j+V[3]]==0&&i+V[2]<=8&&j+V[3]<=8) {i=i+V[2]; j=j+V[3]; return 1;}
- else if(a[i+V[0]][j+V[1]]==0&&i+V[0]>=1&&j+V[1]<=8) {i=i+V[0]; j=j+V[1]; return 1;}
- else if(a[i+E[2]][j+E[3]]==0&&i+E[2]<=8&&j+E[3]>=1) {i=i+E[2]; j=j+E[3]; return 1;}
- else if(a[i+E[0]][j+E[1]]==0&&i+E[0]>=1&&j+E[1]>=1) {i=i+E[0]; j=j+E[1]; return 1;}
- else if(a[i+N[2]][j+N[3]]==0&&i+N[2]>=1&&j+N[3]<=8) {i=i+N[2]; j=j+N[3]; return 1;}
- else if(a[i+N[0]][j+N[1]]==0&&i+N[0]>=1&&j+N[1]>=1) {i=i+N[0]; j=j+N[1]; return 1;}
- }
- return 0;
- }
- int bac(int i,int j)
- {
- while(sol(i,j)==0&&ok==0&&p<64)
- {
- if(valid(i,j))
- {
- in(i,j);
- a[i][j]++;
- }
- else bac(i,j);
- }
- i1=ii;
- j1=ji;
- while(sol1(i,j)==0&&p<64)
- {
- if(valid(i,j))
- {
- in(i,j);
- a[i][j]++;
- }
- else bac(i,j);
- }
- }
- int main()
- {
- int n,i,j;
- char c;
- f>>n>>c;
- if(c=='a') {a[n][1]=1; i=n, j=1;}
- else if(c=='b') {a[n][2]=1; i=n, j=2;}
- else if(c=='c') {a[n][3]=1; i=n,j=3;}
- else if(c=='d') {a[n][4]=1; i=n, j=4;}
- else if(c=='e') {a[n][5]=1; i=n,j=5;}
- else if(c=='f') {a[n][6]=1; i=n,j=6;}
- else if(c=='g') {a[n][7]=1; i=n,j=7;}
- else if(c=='h') {a[n][8]=1; i=n,j=8;}
- f>>n>>c;
- if(c=='a') {a[n][1]=1; i1=n, j1=1;}
- else if(c=='b') {a[n][2]=1; i1=n, j1=2;}
- else if(c=='c') {a[n][3]=1; i1=n,j1=3;}
- else if(c=='d') {a[n][4]=1; i1=n, j1=4;}
- else if(c=='e') {a[n][5]=1; i1=n,j1=5;}
- else if(c=='f') {a[n][6]=1; i1=n,j1=6;}
- else if(c=='g') {a[n][7]=1; i1=n,j1=7;}
- else if(c=='h') {a[n][8]=1; i1=n,j1=8;}
- while(f>>n)
- {
- f>>c;
- if(c=='a') a[n][1]=9;
- else if(c=='b') a[n][2]=9;
- else if(c=='c') a[n][3]=9;
- else if(c=='d') a[n][4]=9;
- else if(c=='e') a[n][5]=9;
- else if(c=='f') a[n][6]=9;
- else if(c=='g') a[n][7]=9;
- else if(c=='h') a[n][8]=9;
- }
- ii=i; ji=j;
- bac(i,j);
- if(ok)
- {
- g<<i<<" ";
- if(j==1) g<<'a'<<endl;
- else if(j==2) g<<'b'<<endl;
- else if(j==3) g<<'c'<<endl;
- else if(j==4) g<<'d'<<endl;
- else if(j==5) g<<'e'<<endl;
- else if(j==6) g<<'f'<<endl;
- else if(j==7) g<<'g'<<endl;
- else if(j==8) g<<'h'<<endl;
- for(i=0;i<l;i++)
- {
- g<<x[i]/10<<" ";
- if(x[i]%10==1) g<<'a'<<endl;
- else if(x[i]%10==2) g<<'b'<<endl;
- else if(x[i]%10==3) g<<'c'<<endl;
- else if(x[i]%10==4) g<<'d'<<endl;
- else if(x[i]%10==5) g<<'e'<<endl;
- else if(x[i]%10==6) g<<'f'<<endl;
- else if(x[i]%10==7) g<<'g'<<endl;
- else if(x[i]%10==8) g<<'h'<<endl;
- }
- }
- else g<<"IMPOSIBIL";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement