martes, 31 de mayo de 2016

Problemas Programacion

CODEFORCES
Problema de numeracion bases cuidado con los overflows
http://www.codeforces.com/contest/602/problem/A

// SRM 590 DIV2-I
problema de string hallar alguna ocurrencia de 5 'o's consecutivos  en linea vertical horizontal o diagonal \ o  diagonal / (n no es menor que 5)

public class  FoxAndGomoku
{
 public string win(string [] b)
 {
   int n = b.Length;
   char[][] bb = new char[n][];
   for(int i = 0; i < n ; i++)
    bb[i] = b[i].ToCharArray();
 
   for(int i = 0; i < n ; i++)
    if( b[i].IndexOf("ooooo")>=0)
      return "found";
   for(int j = 0; j < n ; j++)
   {
   
      for(int i = 0; i < n ; i++)  
      {
        int r = i, s=j, count = 0;
        while( r < n && s >=0)
        {
         if(bb[r][s]=='o')      
          count++;
         else
          break;            
         if(count>=5)
          return "found";
         r++;
        }
      }
   }
 

   for(int i = 0; i < n ; i++)
   {
   
      for(int j = 0; j < n ; j++)        
      {
        int r = i, s=j, count = 0;
        while( r < n && s < n)
        {
         if(bb[r][s]=='o')      
          count++;
         else
          break;          
         if(count>=5)
          return "found";
         r++;s++;
        }      
      }
   }
   for(int i = 0; i < n ; i++)
   {

      for(int j = 0; j < n ; j++)        
      {
        int r = i, s=j, count = 0;
        while( r < n && s >=0)
        {
         if(bb[r][s]=='o')      
          count++;
         else
          break;            
         if(count>=5)
          return "found";
         r++;s--;
        }      
      }
   }

   return  "not found";

 }
}
***************
string win(vector <string> board)
{
    int d[4][2]  = { {1,0}, {0,1}, {1,1}, {-1,1} };
     
    int w = board[0].size(), h = board.size();
    bool anygood = false;
    // For each starting cell:
    for (int i=0; i<w; i++) {
        for (int j=0; j<h; j++) {
            for (int k = 0; k < 4; k++) {
                // load (dx,dy) from array:
                int dx = d[k][0], dy = d[k][1];
                // Is there a line with this direction?
                bool good = true;
                int x = i, y = j;
                for (int k = 0; k < 5; k++) {
                    // make sure the new cell is within bounds:
                    good = (good && (0 <= x && x < w ) && (0 <= y && y < h) );
                    // make sure it contains o
                    good = (good && (board[y][x] == 'o') );
                     
                    // Increase according to (dx,dy).
                    x += dx;
                    y += dy;
                }
                anygood = anygood || good;
            }
        }
    }
    return anygood? "found" : "not found";
}
********************

class FoxAndGomoku:
 def win(self, board):
    h = len(board)
    w = len(board[0])
    # horizontal: For each (x,y) try: (x,y),(x+1,y),...,(x+4,y)
    for x in range(w - 4):
        for y in range(h):
            c = 0
            for i in range(5):
                if board[y][x+i] == 'o':
                    c += 1
            if c == 5:
                return "found"
                 
    # vertical: For each (x,y) try: (x,y),(x,y+1),...,(x,y+4)
    for x in range(w):
        for y in range(h - 4):
            c = 0
            for i in range(5):
                if board[y+i][x] == 'o':
                    c += 1
            if c == 5:
                return "found"
    #diagonal \ : For each (x,y) try: (x,y),(x+1,y+1),...,(x+4,y+4)
    for x in range(w - 4):
        for y in range(h - 4):
            c = 0
            for i in range(5):
                if board[y+i][x+i] == 'o':
                    c += 1
            if c == 5:
                return "found"
     
     
    #diagonal / For each (x,y) try: (x,y),(x-1,y+1),...,(x-4,y+4)
    for x in range(4, w):
        for y in range(h - 4):
            c = 0
            for i in range(5):
                if board[y+i][x-i] == 'o':
                    c += 1
            if c == 5:
                return "found"
    return "not found"

No hay comentarios:

Publicar un comentario