ural 1287. Mars Canals
1287. Mars Canals
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
There is a quadrate area on the Mars surface wreathed in ideal net of canals. They plot the surface into equal squares (we neglect here the surface curvature). Each side of the quadrate area plotted into N square regions.
Archeological investigations showed that there was an ancient country Yatik in this area. The inhabitants cultivated a special grain — sir — that was a basis of their food ration. There is sir of two kinds: coarse-grained and small-grained. As a matter of fact, the end of Yatik empire started after the civil war between the fanciers of the sir sorts. But until recently noone new which of the parties won that time. The scientists look forward to guess the riddle on the grounds of the last voyage to Mars results. They found out which kind of sir was sowed the last in each square of Yatik. According to the ancient tradition sir was sowed in the sequence of squares (parallel to the north-south or east-west directions or at the angle 45° to them), one may suppose that the supporters of the party-winner made the longest sowings.
Input
The first input line contains a size of the square area — N (1 ≤ N ≤ 1400). Then there follow Nlines. Each of them consists of N symbols. A letter “s” in the i-th line and j-th row means that in the according square region small-grained sir was sowed the last, a letter “S” means that coarse-grained sir was sowed the last. You may assume that the inhabitants of the area sowed nothing but sir. Each square region was sowed with only one sort of sir.
Output
The first line should contain a symbol “s”, if the party of small-grained sir fanciers won in the civil war. And symbol “S”, if the winners were the fanciers of the coarse-grained sir. If it’s impossible to define a winner then the first line should contain one symbol “?”. The second line should contain integer number — the maximal length of the one sort of sir sowing.
Samples
input | output |
---|---|
3 SsS sSs SsS |
S 3 |
2 sS Ss |
? 2 |
Problem Author: Alexander Mironenko (prepared by Igor Goldberg)
Problem Source: USU Personal Contest 2004
Problem Source: USU Personal Contest 2004
Tags: dynamic programming
Difficulty: 403
题意:求各个斜线,各个直线上连续的S或s的最大值,然后输出结果。
分析:暴力啊。。。。
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <iostream> 9 #include <algorithm> 10 #include <map> 11 #include <set> 12 #include <ctime> 13 using namespace std; 14 typedef long long LL; 15 typedef double DB; 16 #define For(i, s, t) for(int i = (s); i <= (t); i++) 17 #define Ford(i, s, t) for(int i = (s); i >= (t); i--) 18 #define Rep(i, t) for(int i = (0); i < (t); i++) 19 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--) 20 #define rep(i, x, t) for(int i = (x); i < (t); i++) 21 #define MIT (2147483647) 22 #define INF (1000000001) 23 #define MLL (1000000000000000001LL) 24 #define sz(x) ((int) (x).size()) 25 #define clr(x, y) memset(x, y, sizeof(x)) 26 #define puf push_front 27 #define pub push_back 28 #define pof pop_front 29 #define pob pop_back 30 #define ft first 31 #define sd second 32 #define mk make_pair 33 inline void SetIO(string Name) { 34 string Input = Name+".in", 35 Output = Name+".out"; 36 freopen(Input.c_str(), "r", stdin), 37 freopen(Output.c_str(), "w", stdout); 38 } 39 40 inline int Getint() { 41 int Ret = 0; 42 char Ch = ' '; 43 bool Flag = 0; 44 while(!(Ch >= '0' && Ch <= '9')) { 45 if(Ch == '-') Flag ^= 1; 46 Ch = getchar(); 47 } 48 while(Ch >= '0' && Ch <= '9') { 49 Ret = Ret*10+Ch-'0'; 50 Ch = getchar(); 51 } 52 return Flag ? -Ret : Ret; 53 } 54 55 const int N = 1410; 56 int n; 57 char Map[N][N]; 58 59 inline void Input() { 60 scanf("%d", &n); 61 getchar(); 62 For(i, 1, n) scanf("%s", Map[i]+1); 63 } 64 65 inline void Updata(int &x, int &y) { 66 x = max(x, y); 67 y = 0; 68 } 69 70 inline bool Check(int x, int y) { 71 if(x > n || y > n || x < 1 || y < 1) return 0; 72 return 1; 73 } 74 75 inline void Next(int &x, int &y) { 76 x++, y++; 77 } 78 79 inline int Work(char C) { 80 int Ret = 0; 81 int Tmp = 0; 82 For(i, 1, n) { 83 For(j, 1, n) 84 if(Map[i][j] == C) Tmp++; 85 else Updata(Ret, Tmp); 86 Updata(Ret, Tmp); 87 } 88 89 For(j, 1, n) { 90 For(i, 1, n) 91 if(Map[i][j] == C) Tmp++; 92 else Updata(Ret, Tmp); 93 Updata(Ret, Tmp); 94 } 95 96 For(i, 1, n) { 97 for(int x = 1, y = i; Check(x, y); x++, y++) 98 if(Map[x][y] == C) Tmp++; 99 else Updata(Ret, Tmp); 100 Updata(Ret, Tmp); 101 102 for(int x = i, y = 1; Check(x, y); x++, y++) 103 if(Map[x][y] == C) Tmp++; 104 else Updata(Ret, Tmp); 105 Updata(Ret, Tmp); 106 107 for(int x = n, y = i; Check(x, y); x--, y++) 108 if(Map[x][y] == C) Tmp++; 109 else Updata(Ret, Tmp); 110 Updata(Ret, Tmp); 111 112 for(int x = i, y = 1; Check(x, y); x--, y++) 113 if(Map[x][y] == C) Tmp++; 114 else Updata(Ret, Tmp); 115 Updata(Ret, Tmp); 116 } 117 118 return Ret; 119 } 120 121 inline void Solve() { 122 int Cnt_S = Work('S'); 123 int Cnt_s = Work('s'); 124 if(Cnt_S > Cnt_s) puts("S"); 125 else if(Cnt_S < Cnt_s) puts("s"); 126 else puts("?"); 127 printf("%d\n", max(Cnt_S, Cnt_s)); 128 } 129 130 int main() { 131 #ifndef ONLINE_JUDGE 132 SetIO("E"); 133 #endif 134 Input(); 135 Solve(); 136 return 0; 137 }