AOJ - Problem 0151 : Grid
縦横斜めに連続して1が並ぶ列のうち列の長さが最大のものを出力します。全探索するだけでよいようです。
#include <iostream> #include <string> #include <algorithm> using namespace std; int dx[] = {1,0,1,-1}; int dy[] = {0,1,1,1}; int main(){ int n; while( cin >> n , n ){ string f[256]; int ans = 0; for(int i=0 ; i < n ; i++ ){ cin >> f[i]; } for(int y=0 ; y < n ; y++ ){ for(int x=0 ; x < n ; x++ ){ if( f[y][x] == '1' ){ for(int i=0 ; i < 4 ; i++ ){ int mx = x + dx[i]; int my = y + dy[i]; int cnt = 1; while( mx >= 0 && my >= 0 && mx < n && my < n && f[my][mx] == '1' ){ mx += dx[i]; my += dy[i]; cnt++; } ans = max( ans , cnt ); } } } } cout << ans << endl; } }