Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define nl "\n"
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define ull unsigned ll
- #define RV return void
- #define inf 2000000000
- #define sz(x) int(x.size())
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define Mini(x) *min_element(all(x))
- #define Maxi(x) *max_element(all(x))
- #define fixed(n) fixed << setprecision(n)
- // #define ceil(w, m) (((w) / (m)) + ((w) % (m) ? 1 : 0))
- #define cin(v) for (auto&i:v) cin >> i;
- #define cout(v) for (auto&i:v) cout << i << " ";
- #define clr(memo, x) memset(memo, x, sizeof memo)
- #define updmin(a, b) a = min(a, b)
- #define updmax(a, b) a = max(a, b)
- #define FastCode ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
- vector < string > ternary= {"NO\n" , "YES\n"};
- void Zainab(){
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- }
- /*================================ Prblem solution ================================ */
- void myCode(){
- int n ;
- cin >> n ;
- vector < vector < int > > v (n+1 , vector < int > (n+1 ));
- for(int i =1 ; i <= n ; i++){
- for(int j =1 ; j <= n ; j++) {
- char c;
- cin >> c;
- v[i][j] = c-'0';
- }
- }
- int ans =0;
- vector < pair < int , int > > pre ;
- for(int i =1 ; i <= n ; i++){
- vector < int > curr(n+1 , 0);
- int Sum =0;
- for(int j=1 ;j <= n ; j++){
- if(v[i][j]){
- ans++;
- // curr[max(1 , j-1)]++;
- // curr[min(n , j+1)]++;
- pre.pb({max(1 , j) , min(n , j)});
- v[i][j]=0;
- }
- }
- for(auto& p : pre){
- p.fi -- , p.se++;
- curr[max(1 , p.fi)]++ ;
- curr[min(n , p.se)]++;
- }
- if(i+1 <= n ){
- for(int k =1 ; k <= n ; k++){
- v[i+1][k]+=Sum , v[i+1][k]%=2;
- Sum+= curr[k];
- }
- }
- }
- // cout << sz(pre) << " ";
- cout << ans << nl;
- }
- int main(){
- FastCode ;
- Zainab() ;
- int testCase=1;
- cin >> testCase ;
- for(int i=1 ; i<= testCase ; i++)
- myCode();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement