Codeforces Round #301 (Div. 2) A、C

来源:互联网 发布:纳什均衡 知乎 编辑:程序博客网 时间:2024/04/29 07:40
A. Combination Lock
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock.

The combination lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. In one move, he can rotate one disk one digit forwards or backwards. In particular, in one move he can go from digit 0 to digit 9 and vice versa. What minimum number of actions does he need for that?

Input

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of disks on the combination lock.

The second line contains a string of n digits — the original state of the disks.

The third line contains a string of n digits — Scrooge McDuck's combination that opens the lock.

Output

Print a single integer — the minimum number of moves Scrooge McDuck needs to open the lock.

Sample test(s)
input
58219564723
output
13
Note

In the sample he needs 13 moves:

  • 1 disk: 
  • 2 disk: 
  • 3 disk: 
  • 4 disk: 
  • 5 disk: 
  •  ACcode:
  • #include  <iostream> #include  <cmath> using  namespace std ; #define min ( a , b ) a < b ? a : b int main () {     int n , i , sum = 0 ;    char org [ 1001 ], dst [ 1001 ];    cin >> n ;     cin >> org ;     cin >> dst ;     for ( i = 0 ; i < n ; i ++)     {        sum += min ( abs ( org [ i ]- dst [ i ]), 10 - abs ( org [ i ]- dst [ i ]));     }     cout << sum << endl ;  return  0 ; }

    C. Ice Cave
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice.

    The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked.

    Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c).

    You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this?

    Input

    The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description.

    Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice).

    The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked.

    The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one.

    Output

    If you can reach the destination, print 'YES', otherwise print 'NO'.

    Sample test(s)
    input
    4 6X...XX...XX..X..X.......1 62 2
    output
    YES
    input
    5 4.X.....XX.X......XX.5 31 1
    output
    NO
    input
    4 7..X.XX..XX..X.X...X..X......2 21 6
    output
    YES
    题解BFS
  • ACcode
  • #include <iostream>#include <queue>using namespace std;struct Point{    int x,y;};char map[501][501];int vis[555][555];  int dir[4][2]={1,0,-1,0,0,1,0,-1};  int n,m,sx,sy,ex,ey;bool isInner(int x, int y){      if(x<1 || x>n || y<1 || y>m)          return false;      else          return true;}bool bfs(){    Point next,now;    queue<Point> q;    next.x = sx;    next.y = sy;    vis[sx][sy] = 1;    q.push(next);    while(!q.empty())    {        next = q.front();        q.pop();         if(vis[ex][ey]>=2)         {             return true;         }         for(int i=0; i<4; i++)         {           now.x = next.x+dir[i][0];           now.y = next.y+dir[i][1];           if(isInner(now.x,now.y) &&((map[now.x][now.y]!='X')&& !vis[now.x][now.y])||(now.x==ex && now.y==ey))           {              q.push(now);              vis[now.x][now.y]++;           }         }    }   return false;}int main(){    int i,j;    cin>>n>>m;    for(i=1; i<=n; i++)    {        for(j=1; j<=m; j++)        {            cin>>map[i][j];                if(map[i][j]=='X')                {                   vis[i][j]++;                }        }    }    cin>>sx>>sy>>ex>>ey;    if(bfs())    {        cout<<"YES"<<endl;    }else        cout<<"NO"<<endl; return 0;}


0 0