花生采摘

来源:互联网 发布:大数据和股票有哪些 编辑:程序博客网 时间:2024/04/26 11:17

http://www.acmicpc.sdnu.edu.cn/problem/show/1167
http://codevs.cn/problem/1093/
不知道以前无意中用没用过模拟,但是 这是我印象中的第一次的模拟。这题需要注意的地方有第一次模拟时的比较条件,以及是否是否可以继续模拟的判断条件,尤其是后一条,在进行模拟的时候需要保证去采摘后,能够返回,所以判断条件的时间需要加上x,但是算时间的时候别忘了多减去了还要加上去,具体的看题解吧。

#include<iostream>#include<stdio.h>#include<cmath>#include<string>#include<algorithm>using namespace std;int a[1050][1050] = { 0 };int main(){    int n, m, k;    cin >> n >> m >> k;    for (int i = 1; i < n+1; i++)    {        for (int j = 1; j < m + 1; j++)            scanf_s("%d",&a[i][j]);    }    int daan = 0;    int x, y, xx, yy;    int temp = 1;    while (1)    {        int max = -9999;        for (int i = 1; i < n+1; i++)        {            for (int j = 1; j < m + 1;j++)            if (max<a[i][j])            {                max = a[i][j];                x = i;                y = j;            }        }        if (max==-9999||max==0)        {            break;        }        a[x][y] = 0;        if (temp)        {            xx = 0;            yy = y;            temp = 0;        }        int bushu = abs(xx - x) + abs(yy - y) + x + 1;        if (k>=bushu)        {            daan += max;            k = k - bushu + x;            xx = x;            yy = y;        }else        {            break;        }    }    cout << daan << endl;    system("pause");    return 0;}
0 0
原创粉丝点击