九度OJ 1461-1470(9/9)

1461

#include <iostream>
#include <cstdio>
using namespace std;

#define N 7 

int n, m, t;
char map[N][N+1];

bool legal(int x, int y)
{
    return 0 <= x && x < n && 0 <= y && y < m;
}

bool dfs(int x, int y, int step)
{
    if (step >= t) return false;
    int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    for (int i = 0; i < 4; i ++) {
        int nx = x + d[i][0], ny = y + d[i][1];
        if (!legal(nx, ny) || map[nx][ny] == 'X' || map[nx][ny] == 'S')
            continue;
        if (map[nx][ny] == 'D') {
            if (step == t-1) return true;
            else continue;
        }
        map[nx][ny] = 'X';
        if (dfs(nx, ny, step+1)) return true;
        else map[nx][ny] = '.';
    }
    return false;
}

void solve() {
    int bx, by;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if (map[i][j] == 'S') {
                bx = i; by = j;
            }
        }
    }
    if (dfs(bx, by, 0))
        printf("YES\n");
    else
        printf("NO\n");
}

int main(void)
{
    while (cin >> n >> m >> t) {
        if (!m && !n && !t)
            break;

        for (int i=0; i<n; i++) {
            scanf("%s", map[i]);
        }

        solve();
    }

    return 0;
}
/************************************************************** Problem: 1461 User: liangrx06 Language: C++ Result: Accepted Time:10 ms Memory:1520 kb ****************************************************************/

1462

#include <stdio.h>
#include <string.h>

#define M 5000
#define N 100

int main(void)
{
 int n, c1, c2, sum, i, j;
 int a[N];
 int dp[M+1];

 while (scanf("%d%d%d", &n, &c1, &c2) != EOF)
 {
 sum = 0;
 for(i=0; i<n; i++)
 {
 scanf("%d", &a[i]);
 sum += a[i];
 }

 memset(dp, 0, sizeof(dp));
 for(i=0; i<n; i++)
 {
 for (j=c1; j>=a[i]; j--)
 dp[j] = (dp[j] >= dp[j-a[i]] + a[i]) ? dp[j] : (dp[j-a[i]] + a[i]);
 }

 if (dp[c1] + c2 >= sum)
 printf("YES\n");
 else
 printf("NO\n");
 }

 return 0;
}
/**************************************************************
 Problem: 1462
 User: liangrx06
 Language: C
 Result: Accepted
 Time:20 ms
 Memory:912 kb
****************************************************************/

1463

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define N 1000
#define M 1000

typedef struct node {
    int s;
    int e;
} Meeting;

int cmp(const void *a, const void *b)
{
    return ((Meeting *)a)->e - ((Meeting *)b)->e;
}

int main(void)
{
    int n, i, j;
    Meeting b[N];
    int m[M+1];
    int tmax;

    while (scanf("%d", &n) != EOF && n)
    {
        tmax = 0;
        for(i=0; i<n; i++)
        {
            scanf("%d%d", &b[i].s, &b[i].e);
            tmax = (b[i].e > tmax) ? b[i].e : tmax;
        }
        qsort(b, n, sizeof(b[0]), cmp);

        memset(m, 0, sizeof(m));
        for(i=0; i<n; i++)
        {
            int tmp = m[b[i].s]+1;
            j = b[i].e;
            m[j] = (tmp > m[j]) ? tmp : m[j];
            for (j=b[i].e+1; j<=tmax; j++)
                m[j] = m[j-1];
        }

        printf("%d\n", m[tmax]);
    }

    return 0;
}
/************************************************************** Problem: 1463 User: liangrx06 Language: C Result: Accepted Time:10 ms Memory:912 kb ****************************************************************/

1464

#include <stdio.h>
#include <string.h>

int main()
{
    int i, j, n, n1, n2, n3;
    char s[81];
    char p[50][50];
    while(scanf("%s", s) != EOF)
    {
        n = strlen(s);
        n2 = (n+4)/3;
        if ((n-n2)%2 != 0)
            n2 ++;
        n1 = n3 = (n+2-n2)/2;
        for(i=0; i<n1; i++)
        {
            for (j=0; j<n2; j++)
            {
                if (j == 0)
                    p[i][j] = s[i];
                else if (j == n2-1)
                    p[i][j] = s[n-1-i];
                else if (i == n1-1)
                    p[i][j] = s[j-1+n1];
                else
                    p[i][j] = ' ';
            }
        }
        for(i=0; i<n1; i++)
        {
            for (j=0; j<n2; j++)
            {
                printf("%c", p[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}
/************************************************************** Problem: 1464 User: liangrx06 Language: C Result: Accepted Time:10 ms Memory:912 kb ****************************************************************/

1465

#include <stdio.h>

#define N 600

int check(int a, int b)
{
    if (b == 0)
        return a;
    return check(b, a%b);
}

int main(void)
{
    int n, i, j;
    int a[N];

    while (scanf("%d", &n) != EOF)
    {
        if (n == 0)
            break;

        for(i=0; i<n; i++)
            scanf("%d", &a[i]);

        int count = 0;
        for(i=0; i<n; i++)
        {
            for(j=i+1; j<n; j++)
            {
                int tmp = 2;;
                if (a[i] > a[j])
                    tmp = check(a[i], a[j]);
                else if (a[i] < a[j])
                    tmp = check(a[j], a[i]);
                if (tmp == 1)
                    count ++;
            }
        }

        printf("%d\n", count);
    }

    return 0;
}
/************************************************************** Problem: 1465 User: liangrx06 Language: C Result: Accepted Time:90 ms Memory:912 kb ****************************************************************/

1466

#include <stdio.h>

int fact(int n, int k)
{
 int count = 0;
 int i, j;
 for (i=1; i<=n; i++)
 {
 j = i;
 while (j%k == 0)
 {
 count ++;
 j /= k;
 }
 }
 return count;
}

int main()
{
 int n, m;
 while(scanf("%d%d", &n, &m) != EOF && n)
 {
 printf("%d\n", fact(n, 2)-fact(n-m, 2));
 }
 return 0;
}
/**************************************************************
 Problem: 1466
 User: liangrx06
 Language: C
 Result: Accepted
 Time:20 ms
 Memory:912 kb
****************************************************************/

1467

#include <stdio.h>
#include <stdlib.h>

struct node {
    int key;
    struct node *left;
    struct node *right;
};

struct node *create(struct node *head, int key)
{
    struct node *p;
    if (head == NULL)
    {
        p = (struct node *)malloc(sizeof(struct node));
        p->key = key;
        p->left = NULL;
        p->right = NULL;
        printf("-1\n");
        return p;
    }
    p = head;
    struct node *pfather;
    while (p != NULL)
    {
        pfather = p;
        if (key < p->key)
            p = p->left;
        else
            p = p->right;
    }
    p = (struct node *)malloc(sizeof(struct node));
    p->key = key;
    p->left = NULL;
    p->right = NULL;
    if (key < pfather->key)
        pfather->left = p;
    else
        pfather->right = p;
    printf("%d\n", pfather->key);
    return head;
}

int main(void)
{
    int n, i;
    int a;
    struct node *head = NULL;

    while (scanf("%d", &n) != EOF)
    {
        head = NULL;
        for(i=0; i<n; i++)
        {
            scanf("%d", &a);
            head = create(head, a);
        }
    }

    return 0;
}
/************************************************************** Problem: 1467 User: liangrx06 Language: C Result: Accepted Time:10 ms Memory:912 kb ****************************************************************/

1468

#include <stdio.h>
#include <string.h>

#define N 100000

int main(void)
{
    int n, i;
    int h1, h2;
    char word[N];
    int next[N];
    char c;
    int a, b;

    while (scanf("%d%d%d", &h1, &h2, &n) != EOF)
    {
        for (i=0; i<N; i++)
        {
            next[i] = -1;
            word[i] = '0';
        }
        for (i=0; i<n; i++)
        {
            scanf("%d %c %d", &a, &c, &b);
            word[a] = c;
            next[a] = b;
        }

        char s1[N], s2[N];
        int k1, k2;
        k1 = k2 = 0;
        int j = h1;
        while (j != -1)
        {
            s1[k1++] = word[j];
            j = next[j];
        }
        j = h2;
        while (j != -1)
        {
            s2[k2++] = word[j];
            j = next[j];
        }

        k1 --;
        k2 --;
        if (s1[k1] != s2[k2])
        {
            printf("-1\n");
            continue;
        }
        while (k1>=0 && k2>=0)
        {
            if (s1[k1] != s2[k2])
                break;
            k1 --;
            k2 --;
        }
        k1 ++;
        k2 ++;

        j = h1;
        i = 0;
        while (j != -1)
        {
            if (i == k1)
                break;
            j = next[j];
            i ++;
        }
        printf("%05d\n", j);
    }

    return 0;
}
/**************************************************************
    Problem: 1468
    User: liangrx06
    Language: C
    Result: Accepted
    Time:190 ms
    Memory:1520 kb
****************************************************************/

1470

#include <stdio.h>

#define N 10

void swap(int *a, int *b)
{
    int tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
}

int main()
{
    int i, j, n;
    int a[N][N];
    while(scanf("%d", &n) != EOF) {
        for (i=0; i<n; i++) {
            for (j=0; j<n; j++) {
                scanf("%d", &a[i][j]);
            }
        }

        for (j=0; j<n-1; j++) {
            int max, maxi;
            for (i=j; i<n; i++) {
                if (i == j || a[i][j] > max) {
                    max = a[i][j];
                    maxi = i;
                }
            }
            for (i=0; i<n; i++)
                swap(&a[j][i], &a[maxi][i]);
        }

        for (i=0; i<n; i++) {
            for (j=0; j<n-1; j++) {
                printf("%d ", a[i][j]);
            }
            printf("%d\n", a[i][j]);
        }
    }
    return 0;
}
/**************************************************************
    Problem: 1470
    User: liangrx06
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:912 kb
****************************************************************/

你可能感兴趣的:(九度OJ 1461-1470(9/9))