Tranvick

Untitled

Nov 14th, 2011
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.36 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #define N 1111
  4. using namespace std;
  5.  
  6. struct ceil{
  7.     int v1,v2,h1,h2,x;
  8. } t[N*N];
  9.  
  10. int n,m,k;
  11.  
  12. inline void init(){
  13.     freopen("input.txt","r",stdin);
  14.     freopen("output.txt","w",stdout);
  15.     scanf("%d%d",&n,&m);
  16.     for (int i=2;i<=m+1;i++) t[i]=(ceil){-1,i+m+2,i-1,i+1,-1};
  17.     t[1]=(ceil){-1,m+3,-1,2,-1};
  18.     t[m+2]=(ceil){-1,2*m+4,m+1,-1,-1};
  19.     for (int i=(n+1)*(m+2)+2;i<=(n+2)*(m+2)-1;i++) t[i]=(ceil){i-m-2,-1,i-1,i+1,-1};
  20.     t[(n+1)*(m+2)+1]=(ceil){n*(m+2)+1,-1,-1,(n+1)*(m+2)+2,-1};
  21.     t[(n+2)*(m+2)]=(ceil){(n+1)*(m+2),-1,(n+2)*(m+2)-1,-1,-1};
  22.     for (int i=m+3;i<=n*(m+2)+1;i+=m+2) t[i]=(ceil){i-m-2,i+m+2,-1,i+1,-1};
  23.     for (int i=2*m+4;i<=(n+1)*(m+2);i+=m+2) t[i]=(ceil){i-m-2,i+m+2,i-1,-1,-1};
  24.     int x,num;
  25.     for (int i=1;i<=n;i++)
  26.         for (int j=1;j<=m;j++){
  27.             scanf("%d",&x);
  28.             num=i*(m+2)+j+1;
  29.             t[num]=(ceil){num-m-2,num+m+2,num-1,num+1,x};
  30.         }      
  31.     scanf("%d",&k);
  32. }
  33.  
  34. inline void get(int x1,int y1,int x2,int y2,int &c1,int &c2,int &c3,int &c4,int &l1,int &l2,int &r3,int &r4,int &u1,int &u3,int &d2,int &d4){
  35.     int p,q;
  36.     p=y1+1;c1=t[p].v2;
  37.     for (int i=1;i<x1;i++){
  38.         q=t[c1].v1;
  39.         if (q==p) q=t[c1].v2;
  40.         p=c1;c1=q;
  41.     }
  42.     u1=p;
  43.     p=x1*(m+2)+1;c1=t[p].h2;
  44.     for (int i=1;i<y1;i++){
  45.         q=t[c1].h1;
  46.         if (q==p) q=t[c1].h2;
  47.         p=c1;c1=q;
  48.     }
  49.     l1=p;
  50.     p=(n+1)*(m+2)+y1+1;c2=t[p].v1;
  51.     for (int i=n;i>x2;i--){
  52.         q=t[c2].v1;
  53.         if (q==p) q=t[c2].v2;
  54.         p=c2;c2=q;
  55.     }
  56.     d2=p;
  57.     p=x2*(m+2)+1;c2=t[p].h2;
  58.     for (int i=1;i<y1;i++){
  59.         q=t[c2].h1;
  60.         if (q==p) q=t[c2].h2;
  61.         p=c2;c2=q;
  62.     }
  63.     l2=p;
  64.     p=y2+1;c3=t[p].v2;
  65.     for (int i=1;i<x1;i++){
  66.         q=t[c3].v1;
  67.         if (q==p) q=t[c3].v2;
  68.         p=c3;c3=q;
  69.     }
  70.     u3=p;
  71.     p=(x1+1)*(m+2);c3=t[p].h1;
  72.     for (int i=m;i>y2;i--){
  73.         q=t[c3].h1;
  74.         if (q==p) q=t[c3].h2;
  75.         p=c3;c3=q;
  76.     }
  77.     r3=p;
  78.     p=(n+1)*(m+2)+y2+1;c4=t[p].v1;
  79.     for (int i=n;i>x2;i--){
  80.         q=t[c4].v1;
  81.         if (q==p) q=t[c4].v2;
  82.         p=c4;c4=q;
  83.     }
  84.     d4=p;
  85.     p=(x2+1)*(m+2);c4=t[p].h1;
  86.     for (int i=m;i>y2;i--){
  87.         q=t[c4].h1;
  88.         if (q==p) q=t[c4].h2;
  89.         p=c4;c4=q;
  90.     }
  91.     r4=p;
  92. }
  93.  
  94.    
  95.  
  96. inline void vert(int x1,int y1,int x2,int y2){
  97.     int c1,c2,c3,c4,l1,l2,r3,r4,u1,u3,d2,d4,a1,a2,b1,b2,p1,p2;
  98.     get(x1,y1,x2,y2,c1,c2,c3,c4,l1,l2,r3,r4,u1,u3,d2,d4);
  99.     a1=l1;a2=c1;b1=l2;b2=c2;p1=u1;p2=d2;
  100.     for (int i=1;i<=(x2-x1+1)/2;i++){
  101.         if (t[a2].h1==a1){
  102.             if (t[b2].h1==b1) swap(t[a2].h1,t[b2].h1);
  103.             else swap(t[a2].h1,t[b2].h2);
  104.         }else{
  105.             if (t[b2].h1==b1) swap(t[a2].h2,t[b2].h1);
  106.             else swap(t[a2].h2,t[b2].h2);
  107.         }
  108.         if (t[a1].h1==a2){
  109.             if (t[b1].h1==b2) swap(t[a1].h1,t[b1].h1);
  110.             else swap(t[a1].h1,t[b1].h2);
  111.         }else{
  112.             if (t[b1].h1==b2) swap(t[a1].h2,t[b1].h1);
  113.             else swap(t[a1].h2,t[b1].h2);
  114.         }
  115.         int q=t[a2].v1;
  116.         if (q==p1) q=t[a2].v2;
  117.         p1=a2;a2=q;        
  118.         q=t[b2].v1;
  119.         if (q==p2) q=t[b2].v2;
  120.         p2=b2;b2=q;
  121.             q=t[a1].v1;
  122.         if (t[q].h1!=a2 && t[q].h2!=a2) q=t[a1].v2;
  123.         a1=q;
  124.             q=t[b1].v1;
  125.         if (t[q].h1!=b2 && t[q].h2!=b2) q=t[b1].v2;
  126.         b1=q;
  127.     }
  128.     a1=r3;a2=c3;b1=r4;b2=c4;p1=u3;p2=d4;
  129.     for (int i=1;i<=(x2-x1+1)/2;i++){
  130.         if (t[a2].h1==a1){
  131.             if (t[b2].h1==b1) swap(t[a2].h1,t[b2].h1);
  132.             else swap(t[a2].h1,t[b2].h2);
  133.         }else{
  134.             if (t[b2].h1==b1) swap(t[a2].h2,t[b2].h1);
  135.             else swap(t[a2].h2,t[b2].h2);
  136.         }
  137.         if (t[a1].h1==a2){
  138.             if (t[b1].h1==b2) swap(t[a1].h1,t[b1].h1);
  139.             else swap(t[a1].h1,t[b1].h2);
  140.         }else{
  141.             if (t[b1].h1==b2) swap(t[a1].h2,t[b1].h1);
  142.             else swap(t[a1].h2,t[b1].h2);
  143.         }
  144.         int q=t[a2].v1;
  145.         if (q==p1) q=t[a2].v2;
  146.         p1=a2;a2=q;        
  147.         q=t[b2].v1;
  148.         if (q==p2) q=t[b2].v2;
  149.         p2=b2;b2=q;
  150.             q=t[a1].v1;
  151.         if (t[q].h1!=a2 && t[q].h2!=a2) q=t[a1].v2;
  152.         a1=q;
  153.             q=t[b1].v1;
  154.         if (t[q].h1!=b2 && t[q].h2!=b2) q=t[b1].v2;
  155.         b1=q;
  156.     }  
  157.     a1=u1;a2=c1;
  158.     b1=d2;b2=c2;
  159.     p1=l2;p2=l1;
  160.     for (int i=y1;i<=y2;i++){
  161.         if (t[a2].v1==a1){
  162.             if (t[b2].v1==b1) swap(t[a2].v1,t[b2].v1);
  163.             else swap(t[a2].v1,t[b2].v2);
  164.         }else{
  165.             if (t[b2].v1==b1) swap(t[a2].v2,t[b2].v1);
  166.             else swap(t[a2].v2,t[b2].v2);
  167.         }
  168.         if (t[a1].v1==a2){
  169.             if (t[b1].v1==b2) swap(t[a1].v1,t[b1].v1);
  170.             else swap(t[a1].v1,t[b1].v2);
  171.         }else{
  172.             if (t[b1].v1==b2) swap(t[a1].v2,t[b1].v1);
  173.             else swap(t[a1].v2,t[b1].v2);
  174.         }
  175.         int q=t[a2].h1;
  176.         if (q==p1) q=t[a2].h2;
  177.         p1=a2;a2=q;
  178.         q=t[b2].h1;
  179.         if (q==p2) q=t[b2].h2;
  180.         p2=b2;b2=q;
  181.         q=t[a1].h1;
  182.         if (t[q].v1!=a2 && t[q].v2!=a2) q=t[a1].h2;
  183.         a1=q;
  184.         q=t[b1].h1;
  185.         if (t[q].v1!=b2 && t[q].v2!=b2) q=t[b1].h2;
  186.         b1=q;
  187.     }  
  188.                      
  189. }
  190.  
  191. inline void horiz(int x1,int y1,int x2,int y2){
  192.     int c1,c2,c3,c4,l1,l2,r3,r4,u1,u3,d2,d4,a1,a2,b1,b2,p1,p2;
  193.     get(x1,y1,x2,y2,c1,c2,c3,c4,l1,l2,r3,r4,u1,u3,d2,d4);
  194.     a1=u1;a2=c1;b1=u3;b2=c3;p1=l1;p2=r3;
  195.     for (int i=1;i<=(y2-y1+1)/2;i++){
  196.         if (t[a2].v1==a1){
  197.             if (t[b2].v1==b1) swap(t[a2].v1,t[b2].v1);
  198.             else swap(t[a2].v1,t[b2].v2);
  199.         }else{
  200.             if (t[b2].v1==b1) swap(t[a2].v2,t[b2].v1);
  201.             else swap(t[a2].v2,t[b2].v2);
  202.         }
  203.         if (t[a1].v1==a2){
  204.             if (t[b1].v1==b2) swap(t[a1].v1,t[b1].v1);
  205.             else swap(t[a1].v1,t[b1].v2);
  206.         }else{
  207.             if (t[b1].v1==b2) swap(t[a1].v2,t[b1].v1);
  208.             else swap(t[a1].v2,t[b1].v2);
  209.         }                        
  210.         int q=t[a2].h1;
  211.         if (q==p1) q=t[a2].h2;
  212.         p1=a2;a2=q;        
  213.         q=t[b2].h1;
  214.         if (q==p2) q=t[b2].h2;
  215.         p2=b2;b2=q;
  216.             q=t[a1].h1;
  217.         if (t[q].v1!=a2 && t[q].v2!=a2) q=t[a1].h2;
  218.         a1=q;
  219.             q=t[b1].h1;
  220.         if (t[q].v1!=b2 && t[q].v2!=b2) q=t[b1].h2;
  221.         b1=q;
  222.     }
  223.     a1=d2;a2=c2;b1=d4;b2=c4;p1=l2;p2=r4;
  224.     for (int i=1;i<=(y2-y1+1)/2;i++){
  225.         if (t[a2].v1==a1){
  226.             if (t[b2].v1==b1) swap(t[a2].v1,t[b2].v1);
  227.             else swap(t[a2].v1,t[b2].v2);
  228.         }else{
  229.             if (t[b2].v1==b1) swap(t[a2].v2,t[b2].v1);
  230.             else swap(t[a2].v2,t[b2].v2);
  231.         }
  232.         if (t[a1].v1==a2){
  233.             if (t[b1].v1==b2) swap(t[a1].v1,t[b1].v1);
  234.             else swap(t[a1].v1,t[b1].v2);
  235.         }else{
  236.             if (t[b1].h1==b2) swap(t[a1].v2,t[b1].v1);
  237.             else swap(t[a1].v2,t[b1].v2);
  238.         }
  239.         int q=t[a2].h1;
  240.         if (q==p1) q=t[a2].h2;
  241.         p1=a2;a2=q;        
  242.         q=t[b2].h1;
  243.         if (q==p2) q=t[b2].h2;
  244.         p2=b2;b2=q;
  245.             q=t[a1].h1;
  246.         if (t[q].v1!=a2 && t[q].v2!=a2) q=t[a1].h2;
  247.         a1=q;
  248.             q=t[b1].h1;
  249.         if (t[q].v1!=b2 && t[q].v2!=b2) q=t[b1].h2;
  250.         b1=q;
  251.     }
  252.     a1=l1;a2=c1;b1=r3;b2=c3;p1=u3;p2=u1;
  253.     for (int i=x1;i<=x2;i++){
  254.         if (t[a2].h1==a1){
  255.             if (t[b2].h1==b1) swap(t[a2].h1,t[b2].h1);
  256.             else swap(t[a2].h1,t[b2].h2);
  257.         }else{
  258.             if (t[b2].h1==b1) swap(t[a2].h2,t[b2].h1);
  259.             else swap(t[a2].h2,t[b2].h2);
  260.         }
  261.         if (t[a1].h1==a2){
  262.             if (t[b1].h1==b2) swap(t[a1].h1,t[b1].h1);
  263.             else swap(t[a1].h1,t[b1].h2);
  264.         }else{
  265.             if (t[b1].h1==b2) swap(t[a1].h2,t[b1].h1);
  266.             else swap(t[a1].h2,t[b1].h2);
  267.         }
  268.         int q=t[a2].v1;
  269.         if (q==p1) q=t[a2].v2;
  270.         p1=a2;a2=q;
  271.         q=t[b2].v1;
  272.         if (q==p2) q=t[b2].v2;
  273.         p2=b2;b2=q;
  274.         q=t[a1].v1;
  275.         if (t[q].h1!=a2 && t[q].h2!=a2) q=t[a1].v2;
  276.         a1=q;
  277.         q=t[b1].v1;
  278.         if (t[q].h1!=b2 && t[q].h2!=b2) q=t[b1].v2;
  279.         b1=q;
  280.     }    
  281.  
  282. }
  283.  
  284. void print(){
  285.     for (int i=1;i<=n;i++){
  286.         int p=(m+2)*i+1,q=t[p].h2;
  287.         for (int j=1;j<=m;j++){
  288.             printf("%d",t[q].x);
  289.             if (j!=m) printf(" ");
  290.             if (t[q].h1==p){
  291.                 p=q;q=t[q].h2;
  292.             }else{
  293.                 p=q;q=t[q].h1;
  294.             }
  295.         }
  296.         if (i!=n) printf("\n");
  297.     }
  298. }
  299.  
  300. int main(){
  301.     char c;
  302.     int x1,x2,y1,y2;
  303.     init();
  304.     while (k--){
  305.         scanf("\n%c %d %d %d %d",&c,&x1,&y1,&x2,&y2);
  306.         if (c=='V') vert(x1,y1,x2,y2);
  307.         else horiz(x1,y1,x2,y2);
  308.     }
  309.     print();
  310. /*  for (int i=1;i<=(n+2)*(m+2);i++){ printf("{%d %d %d %d %d %d}, ",i,t[i].v1,t[i].v2,t[i].h1,t[i].h2,t[i].x);
  311.         if (!(i%(m+2))) printf("\n");
  312.     }   */
  313. }
  314.  
Advertisement
Add Comment
Please, Sign In to add comment