#include<ios>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<cmath>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>
using namespace std;
const uint64_t limit=1000000000+7;
/** Maksymalny wymiar planszy */
const int max_size=3002; // +2 bo terminatory
const int max_fields=max_size*max_size;
class Field {
public:
bool empty=false;
/** Ilość sąsiadujących kafelek zajętych */
int con=0;
/** Drugie podejście */
bool scheck=false;
public:
bool isSecondValid() {
if(scheck || con!=0) return false;
// scheck=true;
return true;
}
};
int n,k;
/** Mapa danych */
Field gameMap[max_fields];
inline Field& get(int x, int y) { return gameMap[x+y*max_size]; }
int getCount(int x, int y) {
int res=0;
if(y>1 && get(x,y-1).isSecondValid()) ++res;
if(y<n && get(x,y+1).isSecondValid()) ++res;
if(x>1 && get(x-1, y).isSecondValid()) ++res;
if(x<n && get(x+1, y).isSecondValid()) ++res;
return res;
}
void display() {
for(int y=1;y<=n;++y) {
for(int x=1;x<=n;++x) cerr<<(get(x,y).empty?'.':'#');
cerr<<endl;
}
}
int main(int argc, char** argv) {
// przyspiszenie cin/cout
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// oczyt danych
cin>>n>>k;
string line;
getline(cin, line);
for(int y=1;y<=n;++y) {
getline(cin, line);
for(int x=1;x<=n;++x) {
get(x,y).empty=(line[x-1]=='.');
}
}
for(int i=0;i<(n+2);++i) {
get(0,i).empty=true; // lewy bok
get(n+1, i).empty=true; // prawy
get(i,0).empty=true; // góra
get(i,n+1).empty=true; // dół
}
// display();
/** Osiągalne kafelki na starcie */
int dist1=0;
for(int y=1;y<=n;++y) {
for(int x=1;x<=n;++x) {
if(get(x,y).empty) {
int con = (get(x - 1, y).empty ? 0 : 1) + (get(x + 1, y).empty ? 0 : 1) +
(get(x, y - 1).empty ? 0 : 1) + (get(x, y + 1).empty ? 0 : 1);
get(x, y).con = con;
if (con>0) ++dist1;
} else {
get(x,y).con=-1; // niedostępny
}
}
}
if(k==1) {
cout<<dist1<<endl;
return 0;
}
if(k==2) {
int left=dist1;
int64_t res=0;
for(int y=1;y<=n;++y) {
for(int x=1;x<=n;++x){
Field &f=get(x,y);
if(!f.empty) continue;
if(f.con<=0) continue;
--left;
res+=left+getCount(x,y);
}
}
cout<<(res%limit)<<endl;
return 0;
}
cout<<"0"<<endl;
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #include<ios> #include<iostream> #include<fstream> #include<algorithm> #include<queue> #include<vector> #include<map> #include<cmath> #include<stdlib.h> #include<stdint.h> #include<inttypes.h> using namespace std; const uint64_t limit=1000000000+7; /** Maksymalny wymiar planszy */ const int max_size=3002; // +2 bo terminatory const int max_fields=max_size*max_size; class Field { public: bool empty=false; /** Ilość sąsiadujących kafelek zajętych */ int con=0; /** Drugie podejście */ bool scheck=false; public: bool isSecondValid() { if(scheck || con!=0) return false; // scheck=true; return true; } }; int n,k; /** Mapa danych */ Field gameMap[max_fields]; inline Field& get(int x, int y) { return gameMap[x+y*max_size]; } int getCount(int x, int y) { int res=0; if(y>1 && get(x,y-1).isSecondValid()) ++res; if(y<n && get(x,y+1).isSecondValid()) ++res; if(x>1 && get(x-1, y).isSecondValid()) ++res; if(x<n && get(x+1, y).isSecondValid()) ++res; return res; } void display() { for(int y=1;y<=n;++y) { for(int x=1;x<=n;++x) cerr<<(get(x,y).empty?'.':'#'); cerr<<endl; } } int main(int argc, char** argv) { // przyspiszenie cin/cout ios_base::sync_with_stdio(false); cin.tie(NULL); // oczyt danych cin>>n>>k; string line; getline(cin, line); for(int y=1;y<=n;++y) { getline(cin, line); for(int x=1;x<=n;++x) { get(x,y).empty=(line[x-1]=='.'); } } for(int i=0;i<(n+2);++i) { get(0,i).empty=true; // lewy bok get(n+1, i).empty=true; // prawy get(i,0).empty=true; // góra get(i,n+1).empty=true; // dół } // display(); /** Osiągalne kafelki na starcie */ int dist1=0; for(int y=1;y<=n;++y) { for(int x=1;x<=n;++x) { if(get(x,y).empty) { int con = (get(x - 1, y).empty ? 0 : 1) + (get(x + 1, y).empty ? 0 : 1) + (get(x, y - 1).empty ? 0 : 1) + (get(x, y + 1).empty ? 0 : 1); get(x, y).con = con; if (con>0) ++dist1; } else { get(x,y).con=-1; // niedostępny } } } if(k==1) { cout<<dist1<<endl; return 0; } if(k==2) { int left=dist1; int64_t res=0; for(int y=1;y<=n;++y) { for(int x=1;x<=n;++x){ Field &f=get(x,y); if(!f.empty) continue; if(f.con<=0) continue; --left; res+=left+getCount(x,y); } } cout<<(res%limit)<<endl; return 0; } cout<<"0"<<endl; return 0; } |
English