aryobarzan

USACO castle

Jun 12th, 2011
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. /*
  2. ID:goharsh1
  3. TASK:castle
  4. LANG:C++
  5. */
  6. #include <iostream>
  7. #include <fstream>
  8. #include <algorithm>
  9. using namespace std;
  10.  
  11. ifstream fin("castle.in");
  12. ofstream fout("castle.out");
  13.  
  14. #define cin fin
  15. #define cout fout
  16.  
  17. int castle[100][100];
  18. int roomof[100][100];
  19. int sizer[100*100];
  20. int m,n;
  21.  
  22. int dfs(int i,int j,int room)
  23. {
  24.     if(roomof[i][j]!=0)
  25.         return 0;
  26.     roomof[i][j]=room;
  27.     int size=1;
  28.     int temp=castle[i][j];
  29.     bool n,s,e,w;
  30.     n=s=e=w=true;
  31.     if(temp>=8)
  32.     {
  33.         s=false;
  34.         temp-=8;
  35.     }
  36.     if(temp>=4)
  37.     {
  38.         e=false;
  39.         temp-=4;
  40.     }
  41.     if(temp>=2)
  42.     {
  43.         n=false;
  44.         temp-=2;
  45.     }
  46.     if(temp>=1)
  47.     {
  48.         temp--;
  49.         w=false;
  50.     }
  51.    
  52.     if(s)
  53.         size+=dfs(i+1,j,room);
  54.     if(n)
  55.         size+=dfs(i-1,j,room);
  56.     if(e)
  57.         size+=dfs(i,j+1,room);
  58.     if(w)
  59.         size+=dfs(i,j-1,room);
  60.     sizer[room]=size;
  61.     return size;
  62. }
  63.  
  64. int main()
  65. {
  66.     cin>>m>>n;
  67.     for(int i=0;i<n;i++)
  68.         for(int j=0;j<m;j++)
  69.             cin>>castle[i][j];
  70.     int room=0;//number of rooms
  71.     int maxroom=0;//size of largest room
  72.     for(int i=0;i<n;i++)
  73.         for(int j=0;j<m;j++)
  74.         {
  75.             if(roomof[i][j]==0)
  76.             {
  77.                 maxroom=max(dfs(i,j,++room),maxroom);
  78.             }
  79.         }
  80.     cout<<room<<endl;
  81.     cout<<maxroom<<endl;
  82.     //after wall
  83.     char c='A';
  84.     int maxsize=0;
  85.     int maxi=-1,maxj=-1;
  86.     for(int j=0;j<m;j++)
  87.         for(int i=n-1;i>=0;i--)
  88.         {
  89.             //north
  90.             if(i-1>=0&&roomof[i][j]!=roomof[i-1][j])
  91.             {
  92.                 if(sizer[roomof[i][j]]+sizer[roomof[i-1][j]]>maxsize)
  93.                 {
  94.                     maxsize=sizer[roomof[i][j]]+sizer[roomof[i-1][j]];
  95.                     maxi=i;
  96.                     maxj=j;
  97.                     c='N';
  98.                 }
  99.             }
  100.             //east
  101.             if(j+1<m&&roomof[i][j]!=roomof[i][j+1])
  102.             {
  103.                 if(sizer[roomof[i][j]]+sizer[roomof[i][j+1]]>maxsize)
  104.                 {
  105.                     maxsize=sizer[roomof[i][j]]+sizer[roomof[i][j+1]];
  106.                     maxi=i;
  107.                     maxj=j;
  108.                     c='E';
  109.                 }
  110.             }
  111.            
  112.         }
  113.     cout<<maxsize<<endl;
  114.     cout<<maxi+1<<" "<<maxj+1<<" "<<c<<endl;
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment