|
三个点RE
- #include<iostream>
- #include<deque>
- #include<map>
- using namespace std;
- int n,m,a0,b0,ans,tmp;
- bool b[1001][1001];
- int bb[1001][1001];
- int dx[4]= {0,0,1,-1};
- int dy[4]= {-1,1,0,0};
- struct xy
- {
- int x,y;
- xy(int xx,int yy):x(xx),y(yy) {}
- };
- deque<xy>q;
- map<int,int>mp;
- int main()
- {
- cin>>n>>m;
- string s;
- for(int i=1; i<=n; i++)
- {
- cin>>s;
- for(int j=1; j<=n; j++)b[i][j]=s[j-1]-'0';
- }
- while(m--)
- {
- q.clear();
- cin>>a0>>b0;
- if(bb[a0][b0])
- {
- cout<<mp[bb[a0][b0]]<<endl;
- continue;
- }
- ans=0;
- bb[a0][b0]=++tmp;
- q.push_back(xy(a0,b0));
- while(!q.empty())
- {
- xy t=q.front();
- ans++;
- q.pop_front();
- for(int i=0; i<4; i++)
- {
- int tx=t.x+dx[i];
- int ty=t.y+dy[i];
- if(b[tx][ty]!=b[t.x][t.y]&&!bb[tx][ty]&&tx>=1&&tx<=n&&ty>=1&&ty<=n)
- {
- q.push_back(xy(tx,ty));
- bb[tx][ty]=tmp;
- }
- }
- }
- mp[tmp]=ans;
- cout<<ans<<endl;
- }
- return 0;
- }
复制代码 |
|