Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int matrix[5][5];
- int empty_r,empty_c;
- inline int move(char direction)
- {
- if(direction=='A')
- { if(empty_r==0)
- {
- return 1;
- }
- else
- {
- matrix[empty_r][empty_c]=matrix[empty_r-1][empty_c];
- empty_r--;
- matrix[empty_r][empty_c]=0;
- }
- }
- if(direction=='B')
- {
- if(empty_r==4)
- {
- return 1;
- }
- else
- {
- matrix[empty_r][empty_c]=matrix[empty_r+1][empty_c];
- empty_r++;
- matrix[empty_r][empty_c]=0;
- }
- }
- if(direction=='L')
- {
- if(empty_c==0)
- return 1;
- else
- {
- matrix[empty_r][empty_c]=matrix[empty_r][empty_c-1];
- empty_c--;
- matrix[empty_r][empty_c]=0;
- }
- }
- if(direction=='R')
- {
- if(empty_c==4)
- return 1;
- else
- {
- matrix[empty_r][empty_c]=matrix[empty_r][empty_c+1];
- empty_c++;
- matrix[empty_r][empty_c]=0;
- }
- }
- return 0;
- }
- int main()
- {
- bool flag;
- char dirn[100];
- for(int i=0;i<5;i++)
- for(int j=0;j<5;j++)
- {
- cin>>matrix[i][j];
- if(matrix[i][j]==0)
- {
- empty_c=j;
- empty_r=i;
- }
- }
- cin>>dirn;
- for(int i=0;dirn[i]!='Z';i++)
- {
- int flag_0=move(dirn[i]);
- if(flag_0)
- {
- cout<<"NO FINAL CONFIGURATION POSSIBLE";
- flag=1;
- break;
- }
- //cout<<dirn;
- }
- if(flag==0)
- {
- for(int i=0;i<5;i++)
- {
- for(int j=0;j<5;j++)
- cout<<matrix[i][j]<<" ";
- cout<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement